UnitTest unit tests Compare test results with expected values
(1) Inheritance
(2) Reference package
(3) Execute once test will call setup and teardown
(4) Class name starts with test
(5) Assertion exception
(6) There is a point indicating passing a test case
Second, if there is a unit test execution failure, throw exception or assertion failure, will still be executed. Assertion failure will show E
Any method that starts with test is a method of testing, otherwise it is not performed at the time of the test. Among them, UnitTest. TestCase provides a lot of built-in conditional judgments, and we can call these methods to assert whether the output is what we expect.
Generally there are two kinds, one is assertequal (): Assert that the two results are consistent. One is assertraises (): The specified error type that the assertion is expected to throw.
In addition, you can add two special function setup () and teardown () functions in the unit test, which implements the specified action before and after each call to a test method, where Setup executes before the test function call, and teardown executes afterwards. Each time the use case executes: setup->test_case->teardown; that is, you can use SetUp and TearDown to initialize the environment for each use case
DOME1:
Each time the use case executes: setup->test_case->teardown; that is, you can initialize the environment of each use case with SetUp and TearDown.
1 ImportUnitTest2 3 4 classmytestcase (unittest. TestCase):5 #Pre-built environment6 defsetUp (self):7 Print 'Start Execution' 8 9 #clean up the environmentTen defTearDown (self): One Print 'End Execution' A - #Test Cases - deftest_case1 (self): the Print 'use Case 1' - - #Test Cases - defTest_case2 (self): + Print 'use case 2' - + if __name__=='__main__': AUnittest.main ()
Dome2:
Each class is used once,setupclass->test_case1->test_case2->teardownclass; meaning that using SetupClass, Teardownclass can do all the use cases pre-initialization and end-of-position
1 ImportUnitTest2 3 4 classmytestcase (unittest. TestCase):5 @classmethod6 defSetupClass (CLS):7 Print 'Start Execution' 8 9 @classmethodTen defTeardownclass (CLS): One Print 'End Execution' A - #Test Cases - deftest_case1 (self): the Print 'use Case 1' - - #Test Cases - defTest_case2 (self): + Print 'use case 2' - + if __name__=='__main__': AUnittest.main ()
Third, UnitTest practice
#写一个读文件的类, there is a way to read the entire contents of the file.
#写个单元测试, the assertion file contains the keyword "Glory Road"
main.py
1 ImportUnitTest2 ImportRandom3 fromHandle_fileImportHandlefile4 5 classtestsequencefunctions (unittest. TestCase):6 defTest_run (self):7Content = Handlefile.read_file ("E:\\test4\\ip.txt")8Self.asserttrue ("Glory Road" inchcontent)9 Ten if __name__=="__main__": OneUnittest.main ()
handle_file.py
1 ImportOS2 3 classhandlefile ():4 @staticmethod5 defRead_file (file_path):6 ifos.path.exists (file_path):7 With Open (File_path) as F:8 returnF.read ()9 Else:Ten return "no file" One A - - if __name__=="__main__": thehf=Handlefile () - PrintHf.read_file ("E:\\test4\\ip.txt")
2
"Python" unittest-1