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 ).
- import unittest
- from pprint import pprint
- import feedparser
- class FeedparserTest(unittest.TestCase):
- """
- A test class for the feedparser module.
- """
-
- def setUp(self):
- """
- set up data used in the tests.
- setUp is called before each test function execution.
- """
- self.developerWorksUrl = "testData/developerworks.rss"
- def testParse09Rss(self):
- """
- Test a successful run of the parse function for a
- 0.91 RSS feed.
- """
- print "FeedparserTest.testParse09RSS()"
-
- result = feedparser.parse(self.developerWorksUrl)
- pprint(result)
- self.assertEqual(0, result['bozo'])
-
- self.assert_(result is not None)
- channel = result['channel']
- self.assert_(channel is not None)
- chanDesc = channel['description']
- self.assertEqual(u'The latest content from IBM developerWorks',
- chanDesc)
-
- items = result['items']
- self.assert_(items is not None)
- self.assert_(len(items)> 3)
- firstItem = items[0]
- title = firstItem['title']
- self.assertEqual(u'Build installation packages with
- solution installation and deployment technologies',
- title)
-
- def tearDown(self):
- """
- tear down any data used in tests
- tearDown is called after each test function execution.
- """
- pass
-
- if __name__ == '__main__':
- 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 '.').
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?