unittest commonly used assert statement

Internal Python comes with a module --pyUnit unit test, that is, we say: unittest


first introduce the basic use of unittest:

1. unittest module Import
2. Define a test case class inheritance unittest.TestCase
3. Definitions setUp and tearDown two methods, tearDown process is very important that a clean test environment for future TestCase stay.
4. Define test cases, have to start with the Test
5. a test case just to test one aspect, the purpose of testing and test content to be very clear. Generally used assertEqual, assertIn other assertions to the determination result and the expected result matches a program executed
6. unittest.main call () starts to run test
7. Test all through, does not output anything; If the test fails, it will output appropriate error message.

The following are commonly used unittest assert statement:

assertEqual (A, B, [MSG = 'test fails printed information']): if a = b, then the test case by
assertNotEqual (a, b, [msg = ' test fails printing information ']):! If a = b, then the test case by
assertTrue (x, [msg =' test fails printed information ']): If x is True, then the test case by
assertFalse (x, [ msg = 'test fails printed information']): If x is False, the test case by
assertIs (a, b, [msg = ' test fails printed information']): if a is b, then the test case by
assertNotIs (a, b, [msg = ' test fails printed information']): If A is not a b, then the test case by
assertIsNone (x, [msg = 'test fails printed information']): If x is none, the test case by
assertIsNotNone (x, [msg = 'test fails printed information']): If x is not none, the test case by
assertIn (a, b, [msg = ' information printed test failed' ]): If a in b, then the test case by
assertNotIn (a, b, [msg = ' test fails printed information']): If a is not in (b), the test case by
assertIsInstance (a, b, [ msg = 'test fails printed information']): If a is an instance b of the test by
assertNotIsInstance (a, b, [msg = ' test fails printed information']): If b is not a example, by the test
assertAlmostEqual (A, B): round (ab &,. 7) == 0
assertNotAlmostEqual (A, B):! round (ab &,. 7) = 0
assertGreater (A, B): A> B     
assertGreaterEqual ( A, B): A> = B     
assertLess (A, B): A <B     
assertLessEqual (A, B): A <= B     
assertRegexpMatches (S, Re): regex.search (S)     
assertNotRegexpMatches(s, re):not regex.search(s)     
assertItemsEqual(a, b):sorted(a) == sorted(b) and works with unhashable objs     
assertDictContainsSubset(a, b):all the key/value pairs in a exist in b     
assertMultiLineEqual(a, b):strings     
assertSequenceEqual(a, b):sequences     
assertListEqual(a, b):lists     
assertTupleEqual(a, b):tuples     
assertSetEqual(a, b):sets or frozensets     
assertDictEqual(a, b):dicts     

Guess you like

Origin www.cnblogs.com/laraine/p/11268081.html