DEV Community

Cover image for Python Unittest for Beginners: How to use HTMLTestRunner to generate test reports
Rusydy
Rusydy

Posted on

Python Unittest for Beginners: How to use HTMLTestRunner to generate test reports

In this article, we will go over the process of generating test reports using the unittest framework in Python. We will use the HTMLTestRunner library to generate HTML reports for our tests.

Setup HTMLTestRunner

The HTMLTestRunner library is a third-party library that extends the unittest framework to generate HTML reports for our tests. It is available on GitHub-findyou or GitHub-Rusydy. Just download or copy-paste the file and put it in the same directory as your test file.

Using HTMLTestRunner

To use HTMLTestRunner to create an HTML report for your tests, you need to write your test cases using the unittest library, and then use HTMLTestRunner to run your test cases and generate the HTML report. Here's an example:

  • initiate a test case class that inherits from unittest.TestCase
class MyTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testCase1(self):
        self.assertEqual(2,2,"testError")

    # The following test case will fail
    def testCase2(self):
        self.assertEqual(2,3,"testError")

    def testCase3(self):
        self.assertEqual(2,5,"测试错误")  # en: test error

    def testCase4(self):
        self.assertEqual(2,1,"测试错误")
    # The upper test case will fail

    def testCase5(self):
        pass
Enter fullscreen mode Exit fullscreen mode
  • create a test suite and add the test cases to the test suite
# 添加Suite, en: add Suite
def Suite():

    #定义一个单元测试容器, en: define a unit test container
    suiteTest = unittest.TestSuite()

    #将测试用例加入到容器, en: add test cases to the container
    suiteTest.addTest(MyTestCase("testCase1"))
    suiteTest.addTest(MyTestCase("testCase2"))
    suiteTest.addTest(MyTestCase("testCase3"))
    suiteTest.addTest(MyTestCase("testCase4"))
    suiteTest.addTest(MyTestCase("testCase5"))
    return suiteTest
Enter fullscreen mode Exit fullscreen mode
  • run the test suite using HTMLTestRunner
if __name__ == '__main__':
    with open('test_report.html', 'w') as f: 
        runner = HTMLTestRunner.HTMLTestRunner( # type: ignore
            stream=f, 
            title='测试报告', # en: test report
            description='测试报告') # en: test report
        unittest.main(testRunner=runner)
Enter fullscreen mode Exit fullscreen mode
  • run the test file and generate the HTML report
python3 test_file.py
Enter fullscreen mode Exit fullscreen mode

Replace "test_file.py" with the actual name of your test file. After running the tests, the HTML report will be generated in the same directory with the file name "test_report.html". You can open the report in any web browser to view the test results.

Summary

In this article, we showed you how to set up and use HTMLTestRunner to generate HTML reports for your unit tests. HTMLTestRunner is a useful tool that makes it easy to view the results of your tests in a clear and organized way. By using HTMLTestRunner, you can quickly and easily see which tests passed and which failed, and get a more profound understanding of the overall status of your tests. The full code on this page can be found on GitHub.

Q&A

  • Q: What is the difference between HTMLTestRunner and unittest?
  • A: HTMLTestRunner is a third-party library that extends the unittest framework to generate HTML reports for your tests. It is not part of the standard library.

  • Q: What is setUp and tearDown in unittest?

  • A: The setUp and tearDown methods are used to set up and tear down the test environment. The setUp method is called before each test case is executed, and the tearDown method is called after each test case is executed. You can use the setUp and tearDown methods to set up and tear down the test environment, such as creating a test database, creating a test user, or creating a test file. More information about setUp and tearDown can be found in the unittest documentation.

Latest comments (0)