Skip to content

Add missing __test__ check for discovery #2099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ New Features
Changes
-------

* It is now possible to skip test classes from being collected by setting a
``__test__`` attribute to ``False`` in the class body (`#2007`_). Thanks
to `@syre`_ for the report and `@lwm`_ for the PR.

* Testcase reports with a ``url`` attribute will now properly write this to junitxml.
Thanks `@fushi`_ for the PR (`#1874`_).

Expand Down Expand Up @@ -71,6 +75,7 @@ Changes

*

.. _@syre: https://github.com/syre
.. _@dupuy: https://bitbucket.org/dupuy/
.. _@lwm: https://github.com/lwm
.. _@adler-j: https://github.com/adler-j
Expand All @@ -83,6 +88,7 @@ Changes
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038
.. _#2078: https://github.com/pytest-dev/pytest/issues/2078
.. _#2082: https://github.com/pytest-dev/pytest/issues/2082
.. _#2007: https://github.com/pytest-dev/pytest/issues/2007


3.0.4
Expand Down
2 changes: 1 addition & 1 deletion _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def safe_getattr(object, name, default):
""" Like getattr but return default upon any Exception.

Attribute access can potentially fail for 'evil' Python objects.
See issue214
See issue #214.
"""
try:
return getattr(object, name, default)
Expand Down
2 changes: 2 additions & 0 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ def _get_xunit_func(obj, name):
class Class(PyCollector):
""" Collector for test methods. """
def collect(self):
if not safe_getattr(self.obj, "__test__", True):
return []
if hasinit(self.obj):
self.warn("C1", "cannot collect test class %r because it has a "
"__init__ constructor" % self.obj.__name__)
Expand Down
16 changes: 16 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ def pytest_collect_file(path, parent):
assert len(nodes) == 1
assert isinstance(nodes[0], pytest.File)

def test_can_skip_class_with_test_attr(self, testdir):
"""Assure test class is skipped when using `__test__=False` (See #2007)."""
testdir.makepyfile("""
class TestFoo():
__test__ = False
def __init__(self):
pass
def test_foo():
assert True
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'collected 0 items',
'*no tests ran in*',
])

class TestCollectFS:
def test_ignored_certain_directories(self, testdir):
tmpdir = testdir.tmpdir
Expand Down