Skip to content

Commit 9c224c9

Browse files
authored
Merge pull request #2099 from lwm/fixup-2007
Add missing `__test__` check for discovery
2 parents 36eb5b3 + f5afd8c commit 9c224c9

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ New Features
1313
Changes
1414
-------
1515

16+
* It is now possible to skip test classes from being collected by setting a
17+
``__test__`` attribute to ``False`` in the class body (`#2007`_). Thanks
18+
to `@syre`_ for the report and `@lwm`_ for the PR.
19+
1620
* Testcase reports with a ``url`` attribute will now properly write this to junitxml.
1721
Thanks `@fushi`_ for the PR (`#1874`_).
1822

@@ -71,6 +75,7 @@ Changes
7175

7276
*
7377

78+
.. _@syre: https://github.com/syre
7479
.. _@dupuy: https://bitbucket.org/dupuy/
7580
.. _@lwm: https://github.com/lwm
7681
.. _@adler-j: https://github.com/adler-j
@@ -83,6 +88,7 @@ Changes
8388
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038
8489
.. _#2078: https://github.com/pytest-dev/pytest/issues/2078
8590
.. _#2082: https://github.com/pytest-dev/pytest/issues/2082
91+
.. _#2007: https://github.com/pytest-dev/pytest/issues/2007
8692

8793

8894
3.0.4

_pytest/compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def safe_getattr(object, name, default):
200200
""" Like getattr but return default upon any Exception.
201201
202202
Attribute access can potentially fail for 'evil' Python objects.
203-
See issue214
203+
See issue #214.
204204
"""
205205
try:
206206
return getattr(object, name, default)

_pytest/python.py

+2
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ def _get_xunit_func(obj, name):
503503
class Class(PyCollector):
504504
""" Collector for test methods. """
505505
def collect(self):
506+
if not safe_getattr(self.obj, "__test__", True):
507+
return []
506508
if hasinit(self.obj):
507509
self.warn("C1", "cannot collect test class %r because it has a "
508510
"__init__ constructor" % self.obj.__name__)

testing/test_collection.py

+16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ def pytest_collect_file(path, parent):
8585
assert len(nodes) == 1
8686
assert isinstance(nodes[0], pytest.File)
8787

88+
def test_can_skip_class_with_test_attr(self, testdir):
89+
"""Assure test class is skipped when using `__test__=False` (See #2007)."""
90+
testdir.makepyfile("""
91+
class TestFoo():
92+
__test__ = False
93+
def __init__(self):
94+
pass
95+
def test_foo():
96+
assert True
97+
""")
98+
result = testdir.runpytest()
99+
result.stdout.fnmatch_lines([
100+
'collected 0 items',
101+
'*no tests ran in*',
102+
])
103+
88104
class TestCollectFS:
89105
def test_ignored_certain_directories(self, testdir):
90106
tmpdir = testdir.tmpdir

0 commit comments

Comments
 (0)