Skip to content
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

bpo-32071: Fix regression and add What's New entry #4589

Merged
merged 2 commits into from
Nov 28, 2017
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
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ Added functions :func:`time.thread_time` and :func:`time.thread_time_ns`
to get per-thread CPU time measurements.
(Contributed by Antoine Pitrou in :issue:`32025`.)


unittest
--------
Added new command-line option ``-k`` to filter tests to run with a substring or
Unix shell-like pattern. For example, ``python -m unittest -k foo`` runs the
tests ``foo_tests.SomeTest.test_something``, ``bar_tests.SomeTest.test_foo``,
but not ``bar_tests.FooTest.test_something``.


unittest.mock
-------------

Expand Down
5 changes: 3 additions & 2 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ def getTestCaseNames(self, testCaseClass):
"""Return a sorted sequence of method names found within testCaseClass
"""
def shouldIncludeMethod(attrname):
if not attrname.startswith(self.testMethodPrefix):
return False
testFunc = getattr(testCaseClass, attrname)
isTestMethod = attrname.startswith(self.testMethodPrefix) and callable(testFunc)
if not isTestMethod:
if not callable(testFunc):
return False
fullName = '%s.%s' % (testCaseClass.__module__, testFunc.__qualname__)
return self.testNamePatterns is None or \
Expand Down
23 changes: 23 additions & 0 deletions Lib/unittest/test/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,29 @@ def foobar(self): pass
loader.testNamePatterns = ['*my*']
self.assertEqual(loader.getTestCaseNames(MyTest), [])

# "Return a sorted sequence of method names found within testCaseClass"
#
# If TestLoader.testNamePatterns is set, only tests that match one of these
# patterns should be included.
#
# For backwards compatibility reasons (see bpo-32071), the check may only
# touch a TestCase's attribute if it starts with the test method prefix.
def test_getTestCaseNames__testNamePatterns__attribute_access_regression(self):
class Trap:
def __get__(*ignored):
self.fail('Non-test attribute accessed')

class MyTest(unittest.TestCase):
def test_1(self): pass
foobar = Trap()

loader = unittest.TestLoader()
self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])

loader = unittest.TestLoader()
loader.testNamePatterns = []
self.assertEqual(loader.getTestCaseNames(MyTest), [])

################################################################
### /Tests for TestLoader.getTestCaseNames()

Expand Down