In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to generate unittest test report, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Write the results of the test case to the file
In the test suite, modify the runner used to execute the test suite and add a context manager with statement, for example: with open ('filename', 'wicked') as f, where f represents a stream object, and then add the stream object to the parameter stream of the TextTestRunner class, for example: runner = TextTestRunner (stream=f), and then run it, and a file can be generated in the project, which is the printed content of the test case.
From test1 import MyTestCasefrom unittest import TestSuite, TextTestRunnersuite = TestSuite () # add test cases suite.addTest (MyTestCase ('test_print1')) suite.addTest (MyTestCase (' test_print2')) suite.addTest (MyTestCase ('test_print3')) if _ _ name__ = =' _ main__': # create test runner in the test case set Execute the test suite with open ('html_result1', 'wicked') as f: runner = TextTestRunner (stream=f, verbosity=2) runner.run (suite) 2. Use the html-testRunner module to write the test results to the HTML page
HtmlTest runner is a unit test runner that saves test results in a Html file. First of all, you need to install the module: pip install html-testRunner, which is displayed as HtmlTestRunner when called after installation
When using it, you need to import the HTMLTestRunner class in HtmlTestRunner.
Then replace the TextTestRunner class in the test with the HTMLTestRunner class. Here, you need to pay attention to the parameters included in HTMLTestRunner. Output indicates where to save the results, verbosity displays the printed content of the use case, stream represents standard output, report_name file name, report_title title, descriptions description, and so on. Then run it directly, a html file will be generated in html, and a new html file will be generated if you run it again.
From test import MyTestCasefrom unittest import TestSuite, TextTestRunnerimport HtmlTestRunnersuite = TestSuite () # add test cases suite.addTest (MyTestCase ('test_print1')) suite.addTest (MyTestCase (' test_print2')) suite.addTest (MyTestCase ('test_print3')) if _ _ name__ = =' _ main__': # create test runner in the test case set Execute test suite # runner = TextTestRunner () with open ('html_result',' wicked') as f: runner = HtmlTestRunner.HTMLTestRunner (output='./', stream=f, report_title=' test report', descriptions=' test report details') runner.run (suite)
Let's take a look at the effect of the HTML page in the browser, as described in this test report. For example, the total number of Total of test cases is 3, the number of test cases passed by Pass is 2, and the number of test cases skipped by Skip is 1.
Automatic Discovery based on 3.unittest
We can use the discover () method in the unittest.defaultTestLoader class to automatically find the test case file according to the test directory start_dir match, assemble the found test case into the test suite, and then execute discover through the run () method
From unittest import TextTestRunner, defaultTestLoaderstart_dir ='. / 'discover = defaultTestLoader.discover (start_dir=start_dir, pattern=r'test*.py') if _ _ name__ = =' _ _ main__': # create test runner, execute test suite with open ('html_result1',') as f: runner = TextTestRunner (stream=f, verbosity=2) runner.run (discover) these are all the contents of the article "how unittest generates Test reports" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.