Test when writing a function or class to make sure your code works
The Python module UnitTest provides code testing tools.
Unit tests are used to verify that there is no problem with one aspect of the function;
A test case is a set of unit tests that together verify that a function has been selected in a variety of situations to meet the requirements
#Author:songImportUnitTestclassteststring (unittest. TestCase):defTest_upper (self): self.assertequal ('Foo'. Upper (),'FOO') defTest_isupper (self): Self.asserttrue ('FOO'. Isupper ()) Self.assertfalse ('Foo'. Isupper ())defTest_split (self): s='Hello World'self.assertequal (S.split (), ['Hello',' World']) with Self.assertraises (TypeError): S.split (2)if __name__=="__main__": Unittest.main ()
Unittest.main (): Use it to turn a unit test module into a test script that can be run directly, and the main () method uses the Testloader class to search all the test methods contained in the module that begin with the name "Test" and automatically executes
Python provides a number of assertion methods in the Unittest.testcase class
Common several
UnitTest. The TestCase class contains method Setup (), which allows us to create these objects only once and use them in each test method.
anonymoussurvey.py
classAnonymoussurvey (): #创建匿名调查的类def __init__(self,question): Self.question=question Self.responses= [] defshow_question (self):Print(self.question)defStore_response (self,new_response): Self.responses.append (new_response)defshow_results (self):Print('Survey Result:') forResponseinchself.responses:Print('-'+response)
Test the code,
ImportUnitTestclassTestanonymoussurvey (unittest. TestCase):defsetUp (self): question="What language do you first learn to speak?"Self.my_survey=Anonymoussurvey (question) self.responses= ['中文版','Spanish','Mandarin'] defTest_store_single_response (self): #测试单个答案会被妥善地存储 self.my_survey.store_response (Self.responses[0]) self. Assertin (Self.responses[0], self.my_survey.responses)deftest_store_three_responses (self): #测试多个答案被妥善存储 forResponseinchself.responses:self.my_survey.store_response (response) forResponseinchself.responses:self.assertIn (response, self.my_survey.responses) Unittest.main ()
Method Setup () does two things: Create a Survey object, and create an answer list. The variable name that stores these two things contains the prefix self (which is stored in the attribute), so it can be used anywhere in the class. This makes the two test methods simpler because none of them need to create a survey object and an answer. Method Setup () makes it easier to write test methods, which is much easier than creating an instance and setting its properties in each test method.
Run results
.. ---------------------------------------------------------------------- in 0.000sOK
When you run a test case, Python prints a character every time a unit test is completed: A period is printed when the test passes, an e is printed when the test throws an error, and an F is printed when the test causes the assertion to fail. This is why you see a different period and number of characters in the first line of the output when you run the test case. If the test case contains many unit tests and needs to run for a long time, you can see how many of the tests passed by observing the results
Python test Code