| |
| .. class:: TextTestRunner([stream[, descriptions[, verbosity]]]) |
| |
| A basic test runner implementation which prints results on standard error. It |
| has a few configurable parameters, but is essentially very simple. Graphical |
| applications which run test suites should provide alternate implementations. |
| |
| |
n | .. function:: main([module[, defaultTest[, argv[, testRunner[, testRunner]]]]]) |
n | .. function:: main([module[, defaultTest[, argv[, testRunner[, testLoader]]]]]) |
| |
| A command-line program that runs a set of tests; this is primarily for making |
| test modules conveniently executable. The simplest use for this function is to |
| include the following line at the end of a test script:: |
| |
| if __name__ == '__main__': |
| unittest.main() |
n | |
| The *testRunner* argument can either be a test runner class or an already |
| created instance of it. |
| |
| In some cases, the existing tests may have been written using the :mod:`doctest` |
| module. If so, that module provides a :class:`DocTestSuite` class that can |
| automatically build :class:`unittest.TestSuite` instances from the existing |
| :mod:`doctest`\ -based tests. |
| |
| .. versionadded:: 2.3 |
| |
| the test to be propagated to the caller, and can be used to support running |
| tests under a debugger. |
| |
| The test code can use any of the following methods to check for and report |
| failures. |
| |
| |
| .. method:: TestCase.assert_(expr[, msg]) |
n | XXX Class.failUnless(expr[, msg]) |
n | TestCase.failUnless(expr[, msg]) |
| TestCase.assertTrue(expr[, msg]) |
| |
| Signal a test failure if *expr* is false; the explanation for the error will be |
| *msg* if given, otherwise it will be :const:`None`. |
| |
| |
| .. method:: TestCase.assertEqual(first, second[, msg]) |
n | XXX Class.failUnlessEqual(first, second[, msg]) |
n | TestCase.failUnlessEqual(first, second[, msg]) |
| |
| Test that *first* and *second* are equal. If the values do not compare equal, |
| the test will fail with the explanation given by *msg*, or :const:`None`. Note |
| that using :meth:`failUnlessEqual` improves upon doing the comparison as the |
| first parameter to :meth:`failUnless`: the default value for *msg* can be |
| computed to include representations of both *first* and *second*. |
| |
| |
| .. method:: TestCase.assertNotEqual(first, second[, msg]) |
n | XXX Class.failIfEqual(first, second[, msg]) |
n | TestCase.failIfEqual(first, second[, msg]) |
| |
| Test that *first* and *second* are not equal. If the values do compare equal, |
| the test will fail with the explanation given by *msg*, or :const:`None`. Note |
| that using :meth:`failIfEqual` improves upon doing the comparison as the first |
| parameter to :meth:`failUnless` is that the default value for *msg* can be |
| computed to include representations of both *first* and *second*. |
| |
| |
| .. method:: TestCase.assertAlmostEqual(first, second[, places[, msg]]) |
n | XXX Class.failUnlessAlmostEqual(first, second[, places[, msg]]) |
n | TestCase.failUnlessAlmostEqual(first, second[, places[, msg]]) |
| |
| Test that *first* and *second* are approximately equal by computing the |
n | difference, rounding to the given number of *places*, and comparing to zero. |
n | difference, rounding to the given number of decimal *places* (default 7), |
| and comparing to zero. |
| Note that comparing a given number of decimal places is not the same as |
| comparing a given number of significant digits. If the values do not compare |
| equal, the test will fail with the explanation given by *msg*, or :const:`None`. |
| |
| |
| .. method:: TestCase.assertNotAlmostEqual(first, second[, places[, msg]]) |
n | XXX Class.failIfAlmostEqual(first, second[, places[, msg]]) |
n | TestCase.failIfAlmostEqual(first, second[, places[, msg]]) |
| |
| Test that *first* and *second* are not approximately equal by computing the |
n | difference, rounding to the given number of *places*, and comparing to zero. |
n | difference, rounding to the given number of decimal *places* (default 7), |
| and comparing to zero. |
| Note that comparing a given number of decimal places is not the same as |
| comparing a given number of significant digits. If the values do not compare |
| equal, the test will fail with the explanation given by *msg*, or :const:`None`. |
| |
| |
| .. method:: TestCase.assertRaises(exception, callable, ...) |
n | XXX Class.failUnlessRaises(exception, callable, ...) |
n | TestCase.failUnlessRaises(exception, callable, ...) |
| |
| Test that an exception is raised when *callable* is called with any positional |
| or keyword arguments that are also passed to :meth:`assertRaises`. The test |
| passes if *exception* is raised, is an error if another exception is raised, or |
| fails if no exception is raised. To catch any of a group of exceptions, a tuple |
| containing the exception classes may be passed as *exception*. |
| |
| |
| .. method:: TestCase.failIf(expr[, msg]) |
n | TestCase.assertFalse(expr[, msg]) |
| |
| The inverse of the :meth:`failUnless` method is the :meth:`failIf` method. This |
| signals a test failure if *expr* is true, with *msg* or :const:`None` for the |
| error message. |
| |
| |
| .. method:: TestCase.fail([msg]) |
| |
| The default implementation does nothing. |
| |
| |
| .. method:: TestResult.addError(test, err) |
| |
| Called when the test case *test* raises an unexpected exception *err* is a tuple |
| of the form returned by :func:`sys.exc_info`: ``(type, value, traceback)``. |
| |
n | The default implementation appends ``(test, err)`` to the instance's ``errors`` |
n | The default implementation appends a tuple ``(test, formatted_err)`` to the |
| attribute. |
| instance's ``errors`` attribute, where *formatted_err* is a formatted |
| traceback derived from *err*. |
| |
| |
| .. method:: TestResult.addFailure(test, err) |
| |
| Called when the test case *test* signals a failure. *err* is a tuple of the form |
| returned by :func:`sys.exc_info`: ``(type, value, traceback)``. |
| |
t | The default implementation appends ``(test, err)`` to the instance's |
t | The default implementation appends a tuple ``(test, formatted_err)`` to the |
| ``failures`` attribute. |
| instance's ``failures`` attribute, where *formatted_err* is a formatted |
| traceback derived from *err*. |
| |
| |
| .. method:: TestResult.addSuccess(test) |
| |
| Called when the test case *test* succeeds. |
| |
| The default implementation does nothing. |
| |