def test_skip_non_unittest_class_new_style(self):
@unittest.skip("testing")
class Mixin(object):
def test_1(self):
record.append(1)
class Foo(Mixin, unittest.TestCase):
pass
record = []
result = unittest.TestResult()
test = Foo("test_1")
suite = unittest.TestSuite([test])
suite.run(result)
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])
python类TestResult()的实例源码
def testWeakReferences(self):
# Calling registerResult on a result should not keep it alive
result = unittest2.TestResult()
unittest2.registerResult(result)
ref = weakref.ref(result)
del result
# For non-reference counting implementations
gc.collect();gc.collect()
self.assertIsNone(ref())
def test_multiple_inheritance(self):
class AResult(unittest.TestResult):
def __init__(self, stream, descriptions, verbosity):
super(AResult, self).__init__(stream, descriptions, verbosity)
class ATextResult(unittest.TextTestResult, AResult):
pass
# This used to raise an exception due to TextTestResult not passing
# on arguments in its __init__ super call
ATextResult(None, None, 1)
def testBufferAndFailfast(self):
class Test(unittest2.TestCase):
def testFoo(self):
pass
result = unittest2.TestResult()
runner = unittest2.TextTestRunner(stream=StringIO(), failfast=True,
buffer=True)
# Use our result object
runner._makeResult = lambda: result
runner.run(Test('testFoo'))
self.assertTrue(result.failfast)
self.assertTrue(result.buffer)
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_defaultTestResult(self):
class Foo(unittest2.TestCase):
def runTest(self):
pass
result = Foo().defaultTestResult()
self.assertEqual(type(result), unittest2.TestResult)
# "When a setUp() method is defined, the test runner will run that method
# prior to each test. Likewise, if a tearDown() method is defined, the
# test runner will invoke that method after each test. In the example,
# setUp() was used to create a fresh sequence for each test."
#
# Make sure the proper call order is maintained, even if setUp() raises
# an exception.
def test_subtests_failfast(self):
# Ensure proper test flow with subtests and failfast (issue #22894)
events = []
class Foo(unittest.TestCase):
def test_a(self):
with self.subTest():
events.append('a1')
events.append('a2')
def test_b(self):
with self.subTest():
events.append('b1')
with self.subTest():
self.fail('failure')
events.append('b2')
def test_c(self):
events.append('c')
result = unittest.TestResult()
result.failfast = True
suite = unittest.makeSuite(Foo)
suite.run(result)
expected = ['a1', 'a2', 'b1']
self.assertEqual(events, expected)
# "This class attribute gives the exception raised by the test() method.
# If a test framework needs to use a specialized exception, possibly to
# carry additional information, it must subclass this exception in
# order to ``play fair'' with the framework. The initial value of this
# attribute is AssertionError"
def testSkippingEverywhere(self):
def _skip(self=None):
raise unittest2.SkipTest('some reason')
def nothing(self):
pass
class Test1(unittest2.TestCase):
test_something = _skip
class Test2(unittest2.TestCase):
setUp = _skip
test_something = nothing
class Test3(unittest2.TestCase):
test_something = nothing
tearDown = _skip
class Test4(unittest2.TestCase):
def test_something(self):
self.addCleanup(_skip)
for klass in (Test1, Test2, Test3, Test4):
result = unittest2.TestResult()
klass('test_something').run(result)
self.assertEqual(len(result.skipped), 1)
self.assertEqual(result.testsRun, 1)
def testSystemExit(self):
def _raise(self=None):
raise SystemExit
def nothing(self):
pass
class Test1(unittest2.TestCase):
test_something = _raise
class Test2(unittest2.TestCase):
setUp = _raise
test_something = nothing
class Test3(unittest2.TestCase):
test_something = nothing
tearDown = _raise
class Test4(unittest2.TestCase):
def test_something(self):
self.addCleanup(_raise)
for klass in (Test1, Test2, Test3, Test4):
result = unittest2.TestResult()
klass('test_something').run(result)
self.assertEqual(len(result.errors), 1)
self.assertEqual(result.testsRun, 1)
def test_init__empty_tests(self):
suite = unittest2.TestSuite([])
self.assertEqual(suite.countTestCases(), 0)
# countTestCases() still works after tests are run
suite.run(unittest.TestResult())
self.assertEqual(suite.countTestCases(), 0)
# "class TestSuite([tests])"
# ...
# "If tests is given, it must be an iterable of individual test cases
# or other test suites that will be used to build the suite initially"
#
# TestSuite should allow any iterable to provide tests
def test_init__tests_from_any_iterable(self):
def tests():
yield unittest2.FunctionTestCase(lambda: None)
yield unittest2.FunctionTestCase(lambda: None)
suite_1 = unittest2.TestSuite(tests())
self.assertEqual(suite_1.countTestCases(), 2)
suite_2 = unittest2.TestSuite(suite_1)
self.assertEqual(suite_2.countTestCases(), 2)
suite_3 = unittest2.TestSuite(set(suite_1))
self.assertEqual(suite_3.countTestCases(), 2)
# countTestCases() still works after tests are run
suite_1.run(unittest.TestResult())
self.assertEqual(suite_1.countTestCases(), 2)
suite_2.run(unittest.TestResult())
self.assertEqual(suite_2.countTestCases(), 2)
suite_3.run(unittest.TestResult())
self.assertEqual(suite_3.countTestCases(), 2)
# "class TestSuite([tests])"
# ...
# "If tests is given, it must be an iterable of individual test cases
# or other test suites that will be used to build the suite initially"
#
# Does TestSuite() also allow other TestSuite() instances to be present
# in the tests iterable?
def test_countTestCases_simple(self):
test1 = unittest2.FunctionTestCase(lambda: None)
test2 = unittest2.FunctionTestCase(lambda: None)
suite = unittest2.TestSuite((test1, test2))
self.assertEqual(suite.countTestCases(), 2)
# countTestCases() still works after tests are run
suite.run(unittest.TestResult())
self.assertEqual(suite.countTestCases(), 2)
# "Return the number of tests represented by the this test object.
# ...this method is also implemented by the TestSuite class, which can
# return larger [greater than 1] values"
#
# Make sure this holds for nested TestSuite instances, too
def test_function_in_suite(self):
def f(_):
pass
suite = unittest2.TestSuite()
suite.addTest(f)
# when the bug is fixed this line will not crash
suite.run(unittest2.TestResult())
def test_overriding_call(self):
class MySuite(unittest2.TestSuite):
called = False
def __call__(self, *args, **kw):
self.called = True
unittest2.TestSuite.__call__(self, *args, **kw)
suite = MySuite()
wrapper = unittest2.TestSuite()
wrapper.addTest(suite)
wrapper(unittest2.TestResult())
self.assertTrue(suite.called)
def run(self, test):
result = unittest.TestResult()
test(result)
return result
def run(self, test):
result = unittest.TestResult()
test(result)
return result
def run(self, test):
result = unittest.TestResult()
test(result)
return result
def run(self, test):
result = unittest.TestResult()
test(result)
return result
def run(self, test):
result = unittest.TestResult()
test(result)
return result
def run(self, test):
result = unittest.TestResult()
test(result)
return result