In-depth analysis of the content of the Python testing framework

Source: Internet
Author: User
Tags pprint

The following describes the Python testing framework. Python is an explanatory language, but this statement is incorrect, in fact, he is an easy-to-accept language that allows people who have not learned programming or are not computer-specialized programming.

The Python testing framework has a standard unit testing framework starting with Python 2.3. In Python 2.2, this is only an optional module), similar to the Java jUnit framework. The structure of the test case is the same as that of jUnit. Each class and module to be tested usually has its own test class. The test class contains the test device fixture ).

 
 
  1. import unittest  
  2. from pprint import pprint  
  3. import feedparser  
  4. class FeedparserTest(unittest.TestCase):  
  5.     """  
  6.     A test class for the feedparser module.  
  7.     """  
  8.       
  9.     def setUp(self):  
  10.         """  
  11.         set up data used in the tests.  
  12.         setUp is called before each test function execution.  
  13.         """  
  14.         self.developerWorksUrl = "testData/developerworks.rss"         
  15.     def testParse09Rss(self):  
  16.         """  
  17.         Test a successful run of the parse function for a  
  18.         0.91 RSS feed.  
  19.         """  
  20.         print "FeedparserTest.testParse09RSS()"  
  21.           
  22.         result = feedparser.parse(self.developerWorksUrl)  
  23.         pprint(result)  
  24.         self.assertEqual(0, result['bozo'])  
  25.           
  26.         self.assert_(result is not None)  
  27.         channel = result['channel']  
  28.         self.assert_(channel is not None)  
  29.         chanDesc = channel['description']  
  30.         self.assertEqual(u'The latest content from IBM developerWorks',  
  31.             chanDesc)  
  32.           
  33.         items = result['items']  
  34.         self.assert_(items is not None)  
  35.         self.assert_(len(items)> 3)  
  36.         firstItem = items[0]  
  37.         title = firstItem['title']  
  38.         self.assertEqual(u'Build installation packages with   
  39.             solution installation and deployment technologies',  
  40.             title)  
  41.     
  42.     def tearDown(self):  
  43.         """  
  44.         tear down any data used in tests  
  45.         tearDown is called after each test function execution.  
  46.         """  
  47.         pass  
  48.                   
  49. if __name__ == '__main__':  
  50.     unittest.main()  

They are initialized in the setUp function. Every test is written as an independent test function in the test class. The unittest framework repeats the test function cyclically. call setUp, test the function, and then clear tearDown) to test the function. The above list is a test class for implementing the basic test functions of the feedparser module. For the complete test class, See src/feedParserTest/feedparserTest. py under the FeedparserTest project.

The setUp function is responsible for preparing the test device to be used throughout the test. In this example, only the directory of the RSS file for testing is available. The test function will parse it. TestParse09Rss is a real test function. This function calls the feedparser. parse function to pass the test RSS file and output the parsing result.

The basic check system is executed through the assert function of the TestCase class. If the result of any assert evaluation is not true or any exception is thrown during execution. Unittest reports a test failure or error. The last two lines are responsible for running the test inside the test class by running the module directly.

To run this test class independently, run the FeedparserTest. py module in the same way as described above. Select FeedparserTest. py in the Eclipse Navigator view. Then Run it through the Python testing framework> Run. The STARTUP configuration window is displayed. In addition to the Base Directory, keep the default value for all other items. The Base directory must be the directory of the feedParserTest project.

In this way, you can find the RSS file testData/developerworks. rss in the current directory ). Modify the settings of the base directory and click "Run ". The output information is displayed on the Console. You may want all unit tests we write to be automatically executed as part of the build. Add the build snippets shown in listing 5 to the build script.

The first line is the target declaration, which is the same as other scripts. Lines 2nd to 6th call the py-test task. This part of the code searches for all files ending with "Test. py" in the "src" directory and runs all tests. Set PYTHONPATH to "src", and the current working directory of the test execution is the current directory '.').

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.