Skip to content

Commit

Permalink
Merge pull request #2099 from lwm/fixup-2007
Browse files Browse the repository at this point in the history
Add missing `__test__` check for discovery
  • Loading branch information
The-Compiler authored Nov 30, 2016
2 parents 36eb5b3 + f5afd8c commit 9c224c9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
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

0 comments on commit 9c224c9

Please sign in to comment.