mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
#16935: unittest now counts the module as skipped if it raises SkipTest, instead of counting it as an error. Patch by Zachary Ware.
This commit is contained in:
parent
dacb6858e8
commit
eae2b38948
4 changed files with 43 additions and 5 deletions
|
@ -1493,7 +1493,9 @@ Loading and running tests
|
||||||
directory must be specified separately.
|
directory must be specified separately.
|
||||||
|
|
||||||
If importing a module fails, for example due to a syntax error, then this
|
If importing a module fails, for example due to a syntax error, then this
|
||||||
will be recorded as a single error and discovery will continue.
|
will be recorded as a single error and discovery will continue. If the
|
||||||
|
import failure is due to ``SkipTest`` being raised, it will be recorded
|
||||||
|
as a skip instead of an error.
|
||||||
|
|
||||||
If a test package name (directory with :file:`__init__.py`) matches the
|
If a test package name (directory with :file:`__init__.py`) matches the
|
||||||
pattern then the package will be checked for a ``load_tests``
|
pattern then the package will be checked for a ``load_tests``
|
||||||
|
@ -1512,6 +1514,10 @@ Loading and running tests
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
|
.. versionchanged:: 3.4
|
||||||
|
Modules that raise ``SkipTest`` on import are recorded as skips, not
|
||||||
|
errors.
|
||||||
|
|
||||||
|
|
||||||
The following attributes of a :class:`TestLoader` can be configured either by
|
The following attributes of a :class:`TestLoader` can be configured either by
|
||||||
subclassing or assignment on an instance:
|
subclassing or assignment on an instance:
|
||||||
|
|
|
@ -34,6 +34,14 @@ def _make_failed_test(classname, methodname, exception, suiteClass):
|
||||||
TestClass = type(classname, (case.TestCase,), attrs)
|
TestClass = type(classname, (case.TestCase,), attrs)
|
||||||
return suiteClass((TestClass(methodname),))
|
return suiteClass((TestClass(methodname),))
|
||||||
|
|
||||||
|
def _make_skipped_test(methodname, exception, suiteClass):
|
||||||
|
@case.skip(str(exception))
|
||||||
|
def testSkipped(self):
|
||||||
|
pass
|
||||||
|
attrs = {methodname: testSkipped}
|
||||||
|
TestClass = type("ModuleSkipped", (case.TestCase,), attrs)
|
||||||
|
return suiteClass((TestClass(methodname),))
|
||||||
|
|
||||||
def _jython_aware_splitext(path):
|
def _jython_aware_splitext(path):
|
||||||
if path.lower().endswith('$py.class'):
|
if path.lower().endswith('$py.class'):
|
||||||
return path[:-9]
|
return path[:-9]
|
||||||
|
@ -259,6 +267,8 @@ class TestLoader(object):
|
||||||
name = self._get_name_from_path(full_path)
|
name = self._get_name_from_path(full_path)
|
||||||
try:
|
try:
|
||||||
module = self._get_module_from_name(name)
|
module = self._get_module_from_name(name)
|
||||||
|
except case.SkipTest as e:
|
||||||
|
yield _make_skipped_test(name, e, self.suiteClass)
|
||||||
except:
|
except:
|
||||||
yield _make_failed_import_test(name, self.suiteClass)
|
yield _make_failed_import_test(name, self.suiteClass)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -184,11 +184,9 @@ class TestDiscovery(unittest.TestCase):
|
||||||
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
|
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
|
||||||
self.assertIn(top_level_dir, sys.path)
|
self.assertIn(top_level_dir, sys.path)
|
||||||
|
|
||||||
def test_discover_with_modules_that_fail_to_import(self):
|
def setup_import_issue_tests(self, fakefile):
|
||||||
loader = unittest.TestLoader()
|
|
||||||
|
|
||||||
listdir = os.listdir
|
listdir = os.listdir
|
||||||
os.listdir = lambda _: ['test_this_does_not_exist.py']
|
os.listdir = lambda _: [fakefile]
|
||||||
isfile = os.path.isfile
|
isfile = os.path.isfile
|
||||||
os.path.isfile = lambda _: True
|
os.path.isfile = lambda _: True
|
||||||
orig_sys_path = sys.path[:]
|
orig_sys_path = sys.path[:]
|
||||||
|
@ -198,6 +196,11 @@ class TestDiscovery(unittest.TestCase):
|
||||||
sys.path[:] = orig_sys_path
|
sys.path[:] = orig_sys_path
|
||||||
self.addCleanup(restore)
|
self.addCleanup(restore)
|
||||||
|
|
||||||
|
def test_discover_with_modules_that_fail_to_import(self):
|
||||||
|
loader = unittest.TestLoader()
|
||||||
|
|
||||||
|
self.setup_import_issue_tests('test_this_does_not_exist.py')
|
||||||
|
|
||||||
suite = loader.discover('.')
|
suite = loader.discover('.')
|
||||||
self.assertIn(os.getcwd(), sys.path)
|
self.assertIn(os.getcwd(), sys.path)
|
||||||
self.assertEqual(suite.countTestCases(), 1)
|
self.assertEqual(suite.countTestCases(), 1)
|
||||||
|
@ -206,6 +209,22 @@ class TestDiscovery(unittest.TestCase):
|
||||||
with self.assertRaises(ImportError):
|
with self.assertRaises(ImportError):
|
||||||
test.test_this_does_not_exist()
|
test.test_this_does_not_exist()
|
||||||
|
|
||||||
|
def test_discover_with_module_that_raises_SkipTest_on_import(self):
|
||||||
|
loader = unittest.TestLoader()
|
||||||
|
|
||||||
|
def _get_module_from_name(name):
|
||||||
|
raise unittest.SkipTest('skipperoo')
|
||||||
|
loader._get_module_from_name = _get_module_from_name
|
||||||
|
|
||||||
|
self.setup_import_issue_tests('test_skip_dummy.py')
|
||||||
|
|
||||||
|
suite = loader.discover('.')
|
||||||
|
self.assertEqual(suite.countTestCases(), 1)
|
||||||
|
|
||||||
|
result = unittest.TestResult()
|
||||||
|
suite.run(result)
|
||||||
|
self.assertEqual(len(result.skipped), 1)
|
||||||
|
|
||||||
def test_command_line_handling_parseArgs(self):
|
def test_command_line_handling_parseArgs(self):
|
||||||
program = TestableTestProgram()
|
program = TestableTestProgram()
|
||||||
|
|
||||||
|
|
|
@ -270,6 +270,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #16935: unittest now counts the module as skipped if it raises SkipTest,
|
||||||
|
instead of counting it as an error. Patch by Zachary Ware.
|
||||||
|
|
||||||
- Issue #17018: Make Process.join() retry if os.waitpid() fails with EINTR.
|
- Issue #17018: Make Process.join() retry if os.waitpid() fails with EINTR.
|
||||||
|
|
||||||
- Issue #17197: profile/cProfile modules refactored so that code of run() and
|
- Issue #17197: profile/cProfile modules refactored so that code of run() and
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue