Python 3 Unittest Html And Xml Report Example

The unit test is very useful and helpful in programming. Python provides a built-in unittest module for you to test python class and functions. With third-party modules such as html-testRunner and xmlrunner , you can also generate test case reports in HTML or XML format. This article will tell you how to do that.

1. Create Python Test Case Class And Run Test Method.

  1. If you want to run a unit test in python, you should follow the below steps.
  2. Import python unittest module.
    import unittest
  3. Make your test case class extends unittest.TestCase as below.
    class MyTestExample(unittest.TestCase):
  4. Override unittest.TestCase‘s setUpClass, tearDownClass, setUp, tearDown methods if you need. The first two methods will be run at the class level. And the last two methods will run for each test function.
  5. Define test functions in the class.
  6. Call unittest.main method to run all the test methods.

2. Generate Html Test Reports.

  1. Install python html-testRunner module with below command.
    pip install html-testRunner
  2. Import python HtmlTestRunner module and create Html test runner object.
    import HtmlTestRunner
    testRunner=HtmlTestRunner.HTMLTestRunner(output=html_report_dir)
  3. Pass above test runner object to unittest.main method to run it. Then you can find the output Html report file in the output directory.

3. Generate Xml Test Reports.

  1. Install python xmlrunner module.
    pip install xmlrunner
  2. Import python xmlrunner module and create XMLTestRunner object.
    import xmlrunner
    testRunner=xmlrunner.XMLTestRunner(output=xml_report_dir)
  3. Run the XMLTestRunner object with unittest.main method.
    unittest.main(testRunner)

4. Run Special Test Method With Test Suite.

  1. Create a unittest.TestSuite object.
    test_suite = unittest.TestSuite()
  2. Add test object to test suite.
    test_suite.addTest(MyTestExample(test_function_name))
  3. Create unittest.TestResult object.
    testResult = unittest.TestResult()
  4. Run the test suite with the above TestResult object.

    test_suite.run(testResult)
  5. Get test result data in the testResult object.

5. Run All Test Functions In One Test Class.

  1. Create unittest.TestSuite object.
    test_suite = unittest.TestSuite()
  2. Get all tests in the test class.
    all_test = unittest.makeSuite(test_case_class)
  3. Add all_test to the TestSuite object.
    test_suite.addTest(all_test)
  4. Create unittest.TestResult object.
  5. Run test_suite with the TestResult object.
  6. Get test result data.

6. Run Test Functions In Multiple Test Class File.

  1. This is similar to the above methods. The only difference is that you need to load all the test classes in each .py file and add them to unittest.TestSuite object.
    all_test_cases = unittest.defaultTestLoader.discover('.','*.py')
    # Loop the found test cases and add them into test suite.
    for test_case in all_test_cases:
        test_suite.addTests(test_case)

7. Python Unit Test Generate Html And Xml Reports Examples.

  1. Please see the method comments for code explanation. Below are the project file structures in eclipse PyDev package explorer.python-unittest-example-file-structure
  2. Below is the test case execution generated Html report file.python-html-test-runner-generated-html-test-report
  3. Below is the example full source code.
    import unittest
    import HtmlTestRunner
    import xmlrunner
    
    class MyTestExample(unittest.TestCase):
        '''
        This class demo how to setup test case and run test use python unittest module.
        '''
    
        # This method will be executed only once for this test case class. 
        # It will execute before all test methods. Must decorated with @classmethod.
        @classmethod
        def setUpClass(cls):
            print("setUpClass execute. ")
            
        # Similar with setupClass method, it will be executed after all test method run.    
        @classmethod
        def tearDownClass(cls):
            print("tearDownClass execute. ") 
            
        # This method will be executed before each test function.    
        def setUp(self):
            unittest.TestCase.setUp(self)
            print("setUp method execute. ")
    
        # This method will be executed after each test function.     
        def tearDown(self):
            unittest.TestCase.tearDown(self)
            print("tearDown method execute. ")
    
        def test_function_one(self):
            print("test_function_one execute.")
            self.assertEqual(1, 2, "test_function_one.")
            
        def test_function_two(self):
            print("test_function_two execute.")
            self.assertNotEqual(1, 2, "test_function_two.")
    
    html_report_dir = './html_report' 
    xml_report_dir = './reports/xml_report'      
      
    # Run all test functions.        
    def run_all_test():
        # Run all test functions.
        unittest.main()
        
    # Run all test function and generate html report.    
    def run_all_test_generate_html_report():    
        # Run all test functions with HtmlTestRunner to generate html test report.
        unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=html_report_dir))
        
    # Run all test function and generate html report.    
    def run_all_test_generate_xml_report():    
        # Run all test functions with HtmlTestRunner to generate html test report.
        unittest.main(testRunner=xmlrunner.XMLTestRunner(output=xml_report_dir))           
    
    # Run specified test functions in test suite.        
    def run_test_suite(test_function_name):
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
    
        # Add test function in the suite.
        test_suite.addTest(MyTestExample(test_function_name))
    
        # Run test suite and get test result.
        testResult = unittest.TestResult()
        test_suite.run(testResult)
        print(testResult)
           
    # Run specified test functions and generate html test report.        
    def run_test_suite_generate_html_report(test_function_name):
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
    
        # Add test function in the suite.
        test_suite.addTest(MyTestExample(test_function_name))
    
        # Create HtmlTestRunner object and run the test suite.
        test_runner = HtmlTestRunner.HTMLTestRunner(output=html_report_dir)
        test_runner.run(test_suite)
        
    # Run specified test functions and generate xml test report.        
    def run_test_suite_generate_xml_report(test_function_name):
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
        # Add test function in the suite.
        test_suite.addTest(MyTestExample(test_function_name))
        # Create HtmlTestRunner object and run the test suite.
        test_runner = xmlrunner.XMLTestRunner(output=xml_report_dir)
        test_runner.run(test_suite)    
      
    # Run all test functions in the specified test case class, the function parameter must be a class name not the class name string.
    def run_all_test_in_class(test_case_class):
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
    
        # Make all test function 
        test = unittest.makeSuite(test_case_class)
        test_suite.addTest(test)
    
        # Create a test reslt and run the test suite.
        testResult = unittest.TestResult()
        test_suite.run(testResult)
        print(testResult)
    
    # Run all test functions in the specified test case class, the function parameter must be a class name not the class name string.
    def run_all_test_in_class_generate_html_report(test_case_class):
    
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
    
        # Make all test function 
        test = unittest.makeSuite(test_case_class)
        test_suite.addTest(test)
    
        # Create HtmlTestRunner object and run the test suite.
        test_runner = HtmlTestRunner.HTMLTestRunner(output=html_report_dir)
        test_runner.run(test_suite)
    
    # Run all test functions in the specified test case class, the function parameter must be a class name not the class name string.
    def run_all_test_in_class_generate_xml_report(test_case_class):
        # Create a TestSuite object.
        test_suite = unittest.TestSuite()
        # Make all test function 
        test = unittest.makeSuite(test_case_class)
        test_suite.addTest(test)
        # Create HtmlTestRunner object and run the test suite.
        test_runner = xmlrunner.XMLTestRunner(output=xml_report_dir)
        test_runner.run(test_suite)    
        
    def run_test_in_multiple_module_file():
        # Create test suite.
        test_suite = unittest.TestSuite()
        # Load all test case class in current folder.
        all_test_cases = unittest.defaultTestLoader.discover('.','*.py')
        # Loop the found test cases and add them into test suite.
        for test_case in all_test_cases:
            test_suite.addTests(test_case)      
        
        # Create a test reslt and run the test suite.
        testResult = unittest.TestResult()
        test_suite.run(testResult)
        print(testResult)
      
    def run_test_in_multiple_module_file_generate_html_report():
        # Create test suite.
        test_suite = unittest.TestSuite()
        # Load all test case class in current folder.
        all_test_cases = unittest.defaultTestLoader.discover('.','*.py')
        # Loop the found test cases and add them into test suite.
        for test_case in all_test_cases:
            test_suite.addTests(test_case)      
        
        # Create HtmlTestRunner object and run the test suite.
        test_runner = HtmlTestRunner.HTMLTestRunner(output=html_report_dir)
        test_runner.run(test_suite)   
        
    def run_test_in_multiple_module_file_generate_xml_report():
        # Create test suite.
        test_suite = unittest.TestSuite()
        # Load all test case class in current folder.
        all_test_cases = unittest.defaultTestLoader.discover('.','*.py')
        # Loop the found test cases and add them into test suite.
        for test_case in all_test_cases:
            test_suite.addTests(test_case)      
        
        # Create HtmlTestRunner object and run the test suite.
        test_runner = xmlrunner.XMLTestRunner(output=xml_report_dir)
        test_runner.run(test_suite)           
            
    if __name__=='__main__':
         run_all_test()
         #run_all_test_generate_html_report()
         #run_all_test_generate_xml_report()
         #run_test_suite('test_function_one')
         #run_test_suite_generate_html_report('test_function_two')
         #run_test_suite_generate_xml_report('test_function_two')
         #run_all_test_in_class(MyTestExample)
         #run_all_test_in_class_generate_html_report(MyTestExample)
         #run_all_test_in_class_generate_xml_report(MyTestExample)
         #run_test_in_multiple_module_file()
         #run_test_in_multiple_module_file_generate_html_report()
         #run_test_in_multiple_module_file_generate_xml_report()

8. How To Run Multiple Test Classes In One Test Suite.

  1. If you want to run multiple test classes defined in multiple python files, you can use the python nose library to implement it.
  2. First, install the python nose module with the command pip install nose.
    $ pip install nose
    Collecting nose
      Downloading nose-1.3.7-py3-none-any.whl (154 kB)
         |████████████████████████████████| 154 kB 379 kB/s 
    Installing collected packages: nose
    Successfully installed nose-1.3.7
  3. Put all your test classes python files in one directory.
  4. Run the command nosetests dir-absolute-path/ in the terminal. Then it will run all the test python files in the directory.
  5. If you have multiple directories that are used to save smoke, function, unit test classes files, then you can run the command like below.
    > nosetests smoke-dir/ func-dir/ unit-dir/

Reference

  1. html-testRunner
  2. xmlrunner

10 thoughts on “Python 3 Unittest Html And Xml Report Example”

  1. I am getting below error, while running.. any solution for this..

    # Run all test function and generate html report.
    def test_002_run_all_test_generate_html_report(self):
    
        print("printing")
        # Run all test functions with HtmlTestRunner to generate html test report.
        unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=html_report_dir))
    

    Error
    Traceback (most recent call last):
     File “/testing/tests/report_1.py”, line 50, in test_002_run_all_test_generate_html_report
      unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=html_report_dir))
     File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/main.py”, line 101, in __init__
      self.runTests()
     File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/main.py”, line 273, in runTests
      sys.exit(not self.result.wasSuccessful())
    SystemExit: False

  2. I have several test classes defined in different python files. I want to use HTMLTestRunner to generate one single test result report Html file for the multiple test classes. I follow this article to use one test suite to add the multiple test classes into it. And then use the HTMLTestRunner object to run the test suite. But the the output test report Html file is not a single file, it produce one html report file for one test class. This is not what I want. How can I do that? Who can give me some clue? 

  3. I use the python HTMLTestRunner to create an Html report for unit test functions. And the test program can run complete success, and all the test function passed. But when I open the generated Html report file, there are no data in the Html file. Why? Who can give me some help, thanks.

  4. Is there an example of how can I generate one HTML report which is grouped by Multiple test suite. In the above examples all test cases are part of same test suite

    1. Which function are you using?
      In the example, the function that is executed is one running all the tests without giving out reports…

  5. Hello. I would also like to generate HTML, XML and LOG in one test run too. The above answer does not seem to answer this question. The above example only shows that the test is run through several times.

  6. Hi,

    I am using htmltestrunner, but I am also interested in obtaining xml output for continuous integration reporting.

    Is it possible for one run to generate both outputs?

    Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.