Unittest

From Colettapedia
Jump to navigation Jump to search

References

Quickstart

  1. The class unittest.TestCase is a misnomer... it's really a container for multiple tests that share a self.setUp and self.tearDown
  2. Subclass unittest.TestCase, and write your individual test cases as methods ont that subclass that follow the naming convention test_*
  3. Autogenerate a suite of tests comprised of all the test_* methods you defined on your subclass via:
    • suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
  4. Group all your packages test cases into test suites and group all your test suites into one big test suite.

General testing concepts

  • test fixture = creating temporary databases, directories, starting server processes... the preparation needed to perform one or more tests, and cleanup.
  • test case = Smallest unit of testing. Checks for a specific response to a particular set of inputs
  • test suite = collection of test cases / other suites
  • test runner = test runner, orchestrates execution of tests and provides outcome to user

Classes

  • unittest.TestCase - used when creating new tests
    • setUp() and tearDown() for fixture stuff
    • your tests inherit from class unittest.TestCase
    • either call member functions self.assert___, or use
with self.assertRaises( condition ):
    #do something to cause exception to be raised
  • FunctionTestCase - used to integrate test code within a unittest-driven framework
  • TestSuite

unittest.TestCase

  • Define a new class that inherits from unittest.TestCase
  • Define methods whose names start with the letters 'test_'
  • self.skipTest( reason )

setUp/tearDown

  • Define methods self.setUp and self.tearDown that sets everything up
  • self.addCleanup( func, *args, **kwargs )
    • Cleanup items are called even if setUp fails (unlike tearDown)
  • self.doCleanups()

Call assert* methods from within test_* methods

  • assert_ = assertTrue
  • self.assertAlmostEqual( first, second, places=None, msg=None, delta=None), self.assertNotAlmostEqual
    • default is 7 decimal places
  • self.assertDictContainsSubset(self, expected, actual, msg=None)
    • Checks whether actual is a superset of expected.
  • self.assertDictEqual(self, d1, d2, msg=None)
  • self.assertEqual, assertNotEqual
  • self.assertGreater(self, a, b, msg=None), assertGreaterEqual
    • Just like self.assertTrue(a > b)
  • self.assertLess(self, a, b, msg=None), assertLessEqual
    • Just like self.assertTrue(a < b)
  • self.assertIn, assertNotIn, assertIs, assertIsNot
  • self.assertIsInstance, assertNotIsInstance
  • self.assertIsNone, assertIsNotNone
  • self.assertItemsEqual(self, expected_seq, actual_seq, msg=None)
    • Just compares counts, not items in sequence
  • self.assertListEqual(self, list1, list2, msg=None)
    • is there a ListAlmostEqual?
  • self.assertMultiLineEqual
    • Assert that two multi-line strings are equal
  • assertRegexpMatches, assertNotRegexpMatches(text, regexp)
    • Fail the test if the text matches the regular expression.
  • self.assertRaises( type of the exception, function, arguments )
  • self.assertTrue(all( ... )), assertFalse(any( ... ))
  • self.assertSequenceEqual( seq1, seq2, msg=None, seq_type=None )
  • self.assertSetEqual

Using class and function decorators

  • @unittest.skip(reason)
    • Unconditionally skip the decorated test. reason should describe why the test is being skipped.
  • @unittest.skipIf(condition, reason)
    • Skip the decorated test if condition is true.
  • @unittest.skipUnless(condition, reason)
    • Skip the decorated test unless condition is true.
  • @unittest.expectedFailure
    • Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
  • exception unittest.SkipTest(reason)
    • This exception is raised to skip a test.
    • Usually you can use TestCase.skipTest() or one of the skipping decorators instead of raising this directly.
    • Skipped tests will not have setUp() or tearDown() run around them.
    • Skipped classes will not have setUpClass() or tearDownClass() run.

Calling the test cases

  • inside a test module:
if __name__ == '__main__':
    unittest.main()
  • or:
suite = unittest.TestLoader().loadTestsFromTestCase( name of TestCase)
unittest.TextTestRunner(verbosity=2).run(suite)
  • from command line:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
    • -v verbosity
    • -b buffer output
    • -c catch a Ctrl-C
    • -f failfast die when encountering a failure
  • discover new tests
cd project_directory
python -m unittest discover