Python test code notes for beginners

unit test

Each completed a unit test, Python will print a character:

By printing a test period; testing error caused the printing E; F tests cause an assertion failure Print

Unittest module

unittest Import 
from name_function Import get_formatted_name # import function to be tested 

class NamesTestCase (unittest.TestCase): Note parameter # class oh 
    "" "the beginning of the method name test, the test will be performed automatically." "" 
    
    DEF test_first_last_name (Self): 
        formatted_name = get_formatted_name ( 'Janis',' Joplin ') 
        self.assertEqual (formatted_name,' Janis Joplin ') 
        
    DEF test_first_last_middle_name (Self): 
        formatted_name = get_formatted_name ( 
            ' Wolfgang ',' Mozart ',' Amadeus') 
        self.assertEqual (formatted_name, ' Wolfgang Amadeus Mozart ') # method: assertion 
        # in parentheses to the left with the right side of the comparison are equal no matter, ranging from the alarm 
            

unittest.main () # this file is a test run to make python

 Methods setUP

unittest Import 
from Survey Import AnonymousSurvey 

class TestAnonymousSurvey (unittest.TestCase): 
    "" "Tests for at The class AnonymousSurvey." "" 
    
    DEF the setUp (Self): # TestCase class contains methods setUP Python will first run and then run it to test_ methods prefix 
        "" "is used to create a test instance of an object" "" 
        Question = "the What DID you First Language Learn to Speak?" 
        self.my_survey = AnonymousSurvey (Question) 
        self.responses = [ 'English', 'Spanish', 'Mandarin'] 
        
    
    DEF test_store_single_response (Self): 
        "". "A SINGLE that the Test Stored Response IS Properly" "" 
        self.my_survey.store_response (Self.responses[0])
        self.assertIn(self.responses[0], self.my_survey.responses)
        
        
    def test_store_three_responses(self):
        """Test that three individual responses are stored properly."""
        for response in self.responses:
            self.my_survey.store_response(response)
        for response in self.responses:
            self.assertIn(response, self.my_survey.responses)
            

unittest.main()

 

                                                         

Guess you like

Origin www.cnblogs.com/MR---Zhao/p/12348960.html