Python provides a number of ways to assert in Unittest.testcase .
Assertequal (A, B)---------> Verification a==b
Assertnotequal (A, B)-----> Verification A!=b
Asserttrue (a)-------------> Verification a==true
Assertfalse (a)------------> Verification A==false
Assertin (a,list)-------------> Verify A In List
Assertnotin (a,list)---------> Verify a Not in list
(Well, this is what I read in the book, ^v^)
Cough, the following start the subject, first of all, my English water a pen, so in the name will be Wild way, haha!
A file was created test001.py
The code is as follows:
def test1 (x): return X **2
A boring, simple and clear
OK, then create a test file in the same directory below,test002.py
The code is as follows:
#coding: UTF-8
Import unittestfromimport test1class testnew (unittest. TestCase): " This is a class for testing " def ceshi1 (self): " Test start 1" a=test1 (4) self.assertequal (A, 16)
"This is a test execution statement"
Unittest.main ()
Okay, run the code.
---------------------------------------------------------------------- in 0.000sOK
What ghost, a face confused force, so asked a degree Niang, explanation is the unit test method naming is a rule, that is, test file test002 Def method must start with test, such as test_is_file, no person does not execute. The temper of my clothing
Let's just say change!
#Coding:utf-8ImportUnitTest fromtest001Importtest1classtestnew (unittest. TestCase):" "This is a class for testing" " deftest_ceshi_1 (self):" "Test start 1" "a=test1 (4) Self.assertequal (A,16)if __name__=='__main__': " "This is a test execution statement" "Unittest.main ()
Run again
. ---------------------------------------------------------------------- in 0.000sOK
OK, perfect, this unit is over!
Note, however, that the test results do not appear OK to pass, and may not be executed
Look at the first line of the result of the run, a solid period '. ', all filled with a full stop to indicate execution through
Come, if I change a line of code in test002.py, as follows
A=test1 (4) self.assertequal (A, b) #4** 2 obviously not equal to
Run again
f ===================================================================== =fail:test_ceshi_0 ( __main__ -1---------------------------------------------------------------------- traceback (most recent call last): File " e:\esclipse\test\python_test\test002.py ", line 9, in Test_ceshi_0 self.assertequal (Ce_shi, 15!! =----------------------------------------------------------------------ran 1 Test in 0.000sFAILED ( Failures = 1)
The first line, which shows the 'F', indicates an error
All right, then, add a little bit longer, test001.py.
deftest1 (x):returnX **2deftest2 (name,sal):returnName.title () +','+Str (SAL)deftest3 (x):ifx%2==0:return "True" Else: return "False"deftest4 (x):ifType (x) = =Str:return1Else: return0
test002.py as follows:
1 #Coding:utf-82 ImportUnitTest3 fromtest001ImportTest1,test2,test3,test44 classtestnew (unittest. TestCase):5 " "This is a class for testing" "6 deftest_ceshi_0 (self):7 " "Test Start-1" "8Ce_shi=test1 (4)9Self.assertequal (ce_shi,16)Ten One deftest_ceshi_1 (self): A " "Test 2" " -Ce_shi2=test2 ('Yin', 8000) -Self.assertnotequal (Ce_shi2,'yin8000') the - deftest_ceshi_2 (self): - " "Test 3" " -CE_SHI3=TEST3 (2) + self.asserttrue (CE_SHI3) - + deftest_ceshi_3 (self): A " "Test 4" " atCE_SHI4=TEST3 (2.3) - Self.assertfalse (CE_SHI4) - - defTest_ceshi_4 (self): - " "Test 5" " -Ce_shi5=test4 ('name') inSelf.assertin (ce_shi5,[1,2,3]) - to deftest_ceshi_5 (self): + " "Test 6" " -CE_SHI6=TEST4 (123) theSelf.assertnotin (ce_shi6,[1,2,3]) * $ Panax Notoginseng if __name__=='__main__': -Unittest.main ()
Run the test002.py, please.
1 ... F..2======================================================================3Fail:test_ceshi_3 (__main__. Testnew)4 Test 45----------------------------------------------------------------------6 Traceback (most recent):7File"E:\esclipse\test\python_test\test002.py", line 24,inchTest_ceshi_38 Self.assertfalse (CE_SHI4)9Assertionerror:'False' is notfalseTen One---------------------------------------------------------------------- ARan 6 Testsinch0.001s - -FAILED (Failures=1)
The first line in the result of the run shows: the 4th Test does not pass because Assertionerror: 'false ' is not false
OK, false==0 in Python, so return "False" in test001.py, change to 0, run it again
1 ...... 2 ----------------------------------------------------------------------3 in 0.001s45 OK
Perfect, the above test unit passed, can pass the
Python's unit test-FAQs