1 Introduction to the basic use of the following unittest:
1.import UnitTest
2. Define an inheritance from UnitTest. Test Case Classes for TestCase
3. Define setup and teardown, and do some auxiliary work before and after each test case.
4. Define the test case, with the name beginning with test.
5. A test case should only test one aspect, and the test purpose and test content should be clear. The main is to call Assertequal, assertraises and other assertion methods to determine whether the program execution results and expected values are consistent.
6. Call Unittest.main () to start the test
7. If the test does not pass, the appropriate error message will be output. If all tests pass without displaying anything, you can add the-v parameter to display the details.
Specific examples are as follows:
#-*-coding:cp936-*-
Import UnitTest
Class Defaultwidgetsizetestcase (UnitTest. TestCase):
def setUp (self): #测试前的准备工作
Pass
def test_testcase1 (self): #测试用例1 and must begin with test to be considered a test case
Pass
def test_testcase2 (self):
Pass
def test_testcase3 (self):
Pass
def runtest (self):
Pass
def tearDown (self): #测试后的清除数据工作
Pass
if __name__ = = "__main__":
Unittest.main () #必须调用main启动此次测试
See the source code discovery of UnitTest, which has four testcase,3 in the beginning of test, the other is runtest, but the result only runs 3 testcase at the beginning of test, This is because the code says that if there is a test that starts with testcase, if there is no testcase at the beginning of the test and there is runtest, how to run runtest this test case, in this case, If you comment out the testcase that begin with 3 test, the display runs a testcase, which is runtest.
Test Results:
...
----------------------------------------------------------------------
Ran 3 Tests in 0.078s
OK
2 The following are the common methods of the UnitTest module:
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
3 There are other UnitTest methods for performing more specific checks, such as:
Method Checks this New in
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
Unit Test Example:
Import Random
Import UnitTest
class Testsequencefunctions (unittest. TestCase):
def setUp (self):
self. Seq = range (ten)
def test_shuffle (self):
# Make sure the shuffled sequence does no lose any elements
Random.shuffle (self. seq)
self. Seq.sort ()
self. assertequal (self. Seq, range)
# should raise an exception for an immutable sequence
self. assertraises (TypeError, Random.shuffle, (1,2,3))
def Test_choice (self):
element = Random.choice (self. seq)
Self. Asserttrue (element in self. seq)
def test_sample (self):
With self. assertraises (valueerror):
Random.sample (self. Seq, a)
For element in random.sample (self. Seq, 5):
Self. Asserttrue (element in self. seq)
if __name__ = = ' __main__ ':
Unittest.main ()
Python unittest Basic Introduction