First, why write unit test:
Write the test at the same time will have a deeper understanding of the source code, after all, read the source of the general logic to write tests (or others tell you the function and return value of the code, etc.)
Writing tests is a bit of a waste of emotion, no meaning. No writing function to the jolly. But when you tweak the code or refactor the code, you understand the pain of not testing.
Tests can make the problem easier to locate, and general unit tests are aimed at small blocks of code such as functions or methods, so that you don't get a problem like a headless fly.
Two, Python's unittest module of some nouns (read some crazy, you can take a look at the word remember, follow the example first knock to see the results.) )
Test fixture represents the preparation required to perform one or more tests, as well as any associated cleanup operations. This can involve, for example, creating a temporary or proxy database, a directory, or starting a server process.
The test case is the smallest unit of unit testing. It examines the specific response to a particular input set. UnitTest provides a base class that testcase can use to create new test cases.
Test suite is a collection of test cases,test suites or both. It is used to aggregate the tests that should be performed together.
Test Runner is a component that coordinates test execution and provides results to the user. The performer can use the graphical interface, the text interface, or return special values to indicate the results of the test execution.
Three, the following is the common method of UnitTest module: (Pick the right use, also a few commonly used)
Assertequal (A, b) A = = B
Assertnotequal (A, b) a! = B
Asserttrue (x) bool (x) is True
Assertfalse (x) bool (x) is False
Assertis (A, b) A is B 2.7
Assertisnot (A, B) A is not B 2.7
Assertisnone (x) x is None 2.7
Assertisnotnone (x) x is not None 2.7
Assertin (A, b) a in B 2.7
Assertnotin (A, B) a not in B 2.7
Assertisinstance (A, B) Isinstance (A, B) 2.7
Assertnotisinstance (A, B) not isinstance (A, B) 2.7
Other UnitTest methods ():
Assertalmostequal (A, b) round (A, 7) = = 0
Assertnotalmostequal (A, b) round (A, 7)! = 0
Assertgreater (A, b) a > B 2.7
Assertgreaterequal (A, b) a >= b 2.7
Assertless (A, B) a < b 2.7
Assertlessequal (A, b) a <= b 2.7
Assertregexpmatches (S, re) Regex.search (s) 2.7
Assertnotregexpmatches (S, re) not Regex.search (s) 2.7
Assertitemsequal (A, B) sorted (a) = = sorted (b) and works with Unhashable OBJS 2.7
Assertdictcontainssubset (A, B) all the key/value pairs in a exist in B 2.7
Assertmultilineequal (A, b) strings 2.7
Assertsequenceequal (A, b) sequences 2.7
Assertlistequal (A, b) lists 2.7
Asserttupleequal (A, b) tuples 2.7
Assertsetequal (A, B) sets or Frozensets 2.7
Assertdictequal (A, b) dicts 2.7
Assertmultilineequal (A, b) strings 2.7
Assertsequenceequal (A, b) sequences 2.7
Assertlistequal (A, b) lists 2.7
Asserttupleequal (A, b) tuples 2.7
Assertsetequal (A, B) sets or Frozensets 2.7
Assertdictequal (A, b) dicts 2.7
Not finished: Add now
Python-unitest Module Summary