diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index c9c17d3..cc92a49 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2284,6 +2284,87 @@ class Test_Assertions(TestCase): self.assertRaises(AssertionError, self.failIfAlmostEqual, 0, .1+.1j, places=0) +### Support code for Test_Expectations +################################################################ + +class ExceptionA(Exception): + """Special exception used for tests.""" + +class ExceptionB(Exception): + """Special exception used for tests.""" + +def RaiseException(e): + raise e + +def NoOp(): + pass + +################################################################ +### /Support code for Test_Expectations + +class Test_Expectations(TestCase): + class ExceptionA(Exception): + """Special exception used for tests.""" + + class ExceptionB(Exception): + """Special exception used for tests.""" + + def RaiseException(e): + raise e + + class Expectancies(TestCase): + expectations_count = 7 + + def test_ExpectFail(self): + # Failing expectancies. + self.expectTrue(False) + self.expectFalse(True) + self.expectEqual(1, 2) + self.expectNotEqual(1, 1) + self.expectAlmostEqual(0.0, 1.0) + self.expectNotAlmostEqual(0.0, 0.000000001) + self.expectRaises(ExceptionA, NoOp) + + def test_ExpectPass(self): + # Pasing expectancies. + self.expectTrue(True) + self.expectFalse(False) + self.expectEqual(1, 1) + self.expectNotEqual(1, 2) + self.expectAlmostEqual(0.0, 0.000000001) + self.expectNotAlmostEqual(0.0, 1.0) + self.expectRaises(ExceptionA, RaiseException, ExceptionA) + + def test_ExpectError(self): + self.expectRaises(ExceptionA, RaiseException, ExceptionB) + + def test_ExpectPass(self): + test = self.Expectancies('test_ExpectPass') + #self.failUnlessEqual(test.status, test.NOT_RUN) + result = unittest.TestResult() + test.run(result) + #self.failUnlessEqual(test.status, test.SUCCESS) + self.failUnlessEqual(len(result.errors), 0) + self.failUnlessEqual(len(result.failures), 0) + self.failUnlessEqual(result.testsRun, 1) + + def test_ExpectFail(self): + test = self.Expectancies('test_ExpectFail') + result = unittest.TestResult() + test.run(result) + self.failUnlessEqual(len(result.errors), 0) + self.failUnlessEqual(len(result.failures), + self.Expectancies.expectations_count) + self.failUnlessEqual(result.testsRun, 1) + + def test_ExpectError(self): + test = self.Expectancies('test_ExpectError') + result = unittest.TestResult() + test.run(result) + self.failUnlessEqual(len(result.errors), 1) + self.failUnlessEqual(len(result.failures), 0) + self.failUnlessEqual(result.testsRun, 1) + ###################################################################### ## Main ###################################################################### @@ -2291,7 +2372,7 @@ class Test_Assertions(TestCase): def test_main(): support.run_unittest(Test_TestCase, Test_TestLoader, Test_TestSuite, Test_TestResult, Test_FunctionTestCase, - Test_Assertions) + Test_Assertions, Test_Expectations) if __name__ == "__main__": test_main() diff --git a/Lib/unittest.py b/Lib/unittest.py index 5beeb05..e069942 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -238,6 +238,7 @@ class TestCase: def run(self, result=None): if result is None: result = self.defaultTestResult() + self.result = result result.startTest(self) testMethod = getattr(self, self._testMethodName) try: @@ -287,6 +288,18 @@ class TestCase: """ return sys.exc_info() + def _expect(self, method, *args, **kwargs): + """Run the assertion method but don't fail, just expect.""" + try: + method(*args, **kwargs) + return True + except self.failureException: + # in debug mode there is no result and we just raise expect failures + if not hasattr(self, 'result'): + raise + self.result.addFailure(self, self._exc_info()) + return False + def fail(self, msg=None): """Fail immediately, with the given message.""" raise self.failureException(msg) @@ -295,10 +308,18 @@ class TestCase: "Fail the test if the expression is true." if expr: raise self.failureException(msg) + def expectFalse(self, expr, msg=None): + """Expect the expression to be true, analogous to failIf.""" + self._expect(self.failIf, expr, msg=msg) + def failUnless(self, expr, msg=None): """Fail the test unless the expression is true.""" if not expr: raise self.failureException(msg) + def expectTrue(self, expr, msg=None): + """Expect the expression to be true, analogous to failUnless.""" + self._expect(self.failUnless, expr, msg=msg) + def failUnlessRaises(self, excClass, callableObj, *args, **kwargs): """Fail unless an exception of class excClass is thrown by callableObj when invoked with arguments args and keyword @@ -317,6 +338,11 @@ class TestCase: raise self.failureException("%s not raised by %s" % (excName, objName)) + def expectRaises(self, excClass, callableObj, *args, **kwargs): + """Expect an exception, analogous to failUnlessRaises.""" + self._expect( + self.failUnlessRaises, excClass, callableObj, *args, **kwargs) + def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' operator. @@ -324,6 +350,12 @@ class TestCase: if not first == second: raise self.failureException(msg or '%r != %r' % (first, second)) + def expectEqual(self, first, second, msg=None): + """Expect first and second to be equal by '==', analogous to + failUnlessEqual. + """ + self._expect(self.failUnlessEqual, first, second, msg=msg) + def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. @@ -331,6 +363,12 @@ class TestCase: if first == second: raise self.failureException(msg or '%r == %r' % (first, second)) + def expectNotEqual(self, first, second, msg=None): + """Expect first and second to not be equal by '==', analogous to + failIfEqual. + """ + self._expect(self.failIfEqual, first, second, msg=msg) + def failUnlessAlmostEqual(self, first, second, *, places=7, msg=None): """Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places @@ -343,6 +381,13 @@ class TestCase: raise self.failureException(msg or '%r != %r within %r places' % (first, second, places)) + def expectAlmostEqual(self, first, second, *, places=7, msg=None): + """Expect first and second to be almost equal, analogous to + failUnlessAlmostEqual. + """ + self._expect( + self.failUnlessAlmostEqual, first, second, places=places, msg=msg) + def failIfAlmostEqual(self, first, second, *, places=7, msg=None): """Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places @@ -355,6 +400,13 @@ class TestCase: raise self.failureException(msg or '%r == %r within %r places' % (first, second, places)) + def expectNotAlmostEqual(self, first, second, *, places=7, msg=None): + """Expect first and second to be almost equal, analogous to + failIfAlmostEqual. + """ + self._expect( + self.failIfAlmostEqual, first, second, places=places, msg=msg) + # Synonyms for assertion methods assertEqual = assertEquals = failUnlessEqual