我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest.expectedFailure()。
def testOldTestResult(self): class Test(unittest.TestCase): def testSkip(self): self.skipTest('foobar') @unittest.expectedFailure def testExpectedFail(self): raise TypeError @unittest.expectedFailure def testUnexpectedSuccess(self): pass for test_name, should_pass in (('testSkip', True), ('testExpectedFail', True), ('testUnexpectedSuccess', False)): test = Test(test_name) self.assertOldResultWarning(test, int(not should_pass))
def test_compound_unaffected_compound(self): """Unaffected compound: exclude (the disease is not due to a compound)""" genotypes = array( [[2,1, 2,2, 1,1], [2,1, 2,2, 2,1], [1,2, 2,2, 1,1], [1,2, 2,2, 2,1]], dtype=DTYPE) variants = VariantsCollection([ Variant(variant_id=1, start=0, gene_symbol='B'), # mother 1 Variant(variant_id=2, start=1, gene_symbol='B'), # mother 2 + unaffected 1 Variant(variant_id=3, start=2, gene_symbol='B'), # father 1 Variant(variant_id=4, start=3, gene_symbol='B'), # father 2 + unaffected 2 ]) var = GenotypesFilterCompoundHeterozygous(self.ss).apply(variants, genotypes).variants self.assertEqual(len(var), 2) # would be 4, but here we exclude an entire pair (2 variants): # one of the ends of the unaffected pair must be non-deleterious, # so no affected pair containing one of them can have both alleles deficient. # Thus a single pair remains -> 2 variants. # Updated by the change that allows only one same compound for all affected samples. #@unittest.expectedFailure # To my opinion it is ok if different compound scenarios work in different affected samples # i.e. two samples can be affected but each one with a different compound pair.
def test_expected_failure_with_wrapped_subclass(self): class Foo(unittest.TestCase): def test_1(self): self.assertTrue(False) @unittest.expectedFailure class Bar(Foo): pass events = [] result = LoggingResult(events) test = Bar("test_1") test.run(result) self.assertEqual(events, ['startTest', 'addExpectedFailure', 'stopTest']) self.assertEqual(result.expectedFailures[0][0], test) self.assertTrue(result.wasSuccessful())
def test_expected_failure_subtests(self): # A failure in any subtest counts as the expected failure of the # whole test. class Foo(unittest.TestCase): @unittest.expectedFailure def test_die(self): with self.subTest(): # This one succeeds pass with self.subTest(): self.fail("help me!") with self.subTest(): # This one doesn't get executed self.fail("shouldn't come here") events = [] result = LoggingResult(events) test = Foo("test_die") test.run(result) self.assertEqual(events, ['startTest', 'addSubTestSuccess', 'addExpectedFailure', 'stopTest']) self.assertEqual(len(result.expectedFailures), 1) self.assertIs(result.expectedFailures[0][0], test) self.assertTrue(result.wasSuccessful())
def test_unexpected_success_subtests(self): # Success in all subtests counts as the unexpected success of # the whole test. class Foo(unittest.TestCase): @unittest.expectedFailure def test_die(self): with self.subTest(): # This one succeeds pass with self.subTest(): # So does this one pass events = [] result = LoggingResult(events) test = Foo("test_die") test.run(result) self.assertEqual(events, ['startTest', 'addSubTestSuccess', 'addSubTestSuccess', 'addUnexpectedSuccess', 'stopTest']) self.assertFalse(result.failures) self.assertEqual(result.unexpectedSuccesses, [test]) self.assertFalse(result.wasSuccessful())
def test_no_exception_leak(self): # Issue #19880: TestCase.run() should not keep a reference # to the exception class MyException(Exception): ninstance = 0 def __init__(self): MyException.ninstance += 1 Exception.__init__(self) def __del__(self): MyException.ninstance -= 1 class TestCase(unittest.TestCase): def test1(self): raise MyException() @unittest.expectedFailure def test2(self): raise MyException() for method_name in ('test1', 'test2'): testcase = TestCase(method_name) testcase.run() self.assertEqual(MyException.ninstance, 0)
def test_on_message_when_nonexistent_event(self): ws_connection = yield self.ws_connect('/ws/test') # Test when the event is not binded self.assertDictEqual(self.ws.events, {'hello': ANY}) ws_connection.write_message(json_encode({ 'event': 'bye', 'data': {'message': 'Bye !'} })) # Throw TimeoutError and/or StopIteration, because WebSocketHandler.on_message() does not send # any message when the client send a nonexistent event ('bye' in our case). # Also, I don't know how catch them, because self.assertRaises() and try/catch don't work. # So I use @unittest.expectedFailure here, but it's a bit dirty imo. yield ws_connection.read_message()
def test_unittest_unexpected_failure(testdir): testdir.makepyfile(""" import unittest class MyTestCase(unittest.TestCase): @unittest.expectedFailure def test_func1(self): assert 0 @unittest.expectedFailure def test_func2(self): assert 1 """) result = testdir.runpytest("-rxX") result.stdout.fnmatch_lines([ "*XFAIL*MyTestCase*test_func1*", "*XPASS*MyTestCase*test_func2*", "*1 xfailed*1 xpass*", ])
def expectedFailureIfAppengine(func): try: from google import appengine # noqa except ImportError: return func else: import os if os.environ.get('SERVER_SOFTWARE', None) is None: return func return unittest.expectedFailure(func)
def test_expected_failure(self): class Foo(unittest.TestCase): @unittest.expectedFailure def test_die(self): self.fail("help me!") events = [] result = LoggingResult(events) test = Foo("test_die") test.run(result) self.assertEqual(events, ['startTest', 'addExpectedFailure', 'stopTest']) self.assertEqual(result.expectedFailures[0][0], test) self.assertTrue(result.wasSuccessful())
def test_unexpected_success(self): class Foo(unittest.TestCase): @unittest.expectedFailure def test_die(self): pass events = [] result = LoggingResult(events) test = Foo("test_die") test.run(result) self.assertEqual(events, ['startTest', 'addUnexpectedSuccess', 'stopTest']) self.assertFalse(result.failures) self.assertEqual(result.unexpectedSuccesses, [test]) self.assertTrue(result.wasSuccessful())
def compile_and_run_file_failing_test(*a, **k): """Turn a test to a failing test""" _class = compile_and_run_file_test(*a, **k) class FailingTest(_class): """Failing test""" @unittest.expectedFailure def runTest(self): return super(FailingTest, self).runTest() return FailingTest
def expectedFailurePY3(func): if not PY3: return func return unittest.expectedFailure(func)
def expectedFailurePY26(func): if not PY26: return func return unittest.expectedFailure(func)
def expectedFailurePY27(func): if not PY27: return func return unittest.expectedFailure(func)
def expectedFailurePY2(func): if not PY2: return func return unittest.expectedFailure(func) # Renamed in Py3.3:
def anticipate_failure(condition): """Decorator to mark a test that is known to be broken in some cases Any use of this decorator should have a comment identifying the associated tracker issue. """ if condition: return unittest.expectedFailure return lambda f: f
def test_2(self): """Illustrate how to use unittest.expectedFailure""" print("in test_2") self.assertEqual(1 + 1, 3) #@unittest.skip("Skipping test_1")
def test_2(self): """Illustrate how to use unittest.expectedFailure""" print("in test_2") self.assertEqual(1 + 1, 3)