Python 3 Unittest Html And Xml Report Example

Unit test is very useful and helpful in programming. Python provide 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.

If you want to run unit test in python, you should follow below steps.

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

2. Generate Html Test Reports.

  1. Install html-testRunner module with below command.
    pip install html-testRunner
  2. Import HtmlTestRunner 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 xmlrunner module.
    pip install xmlrunner
  2. Import xmlrunner and create xml test runner object.
    import xmlrunner
    testRunner=xmlrunner.XMLTestRunner(output=xml_report_dir)
  3. Run the xml test runner object with unittest.main method.
    unittest.main(testRunner)

4. Run Special Test Method With Test Suite.

  1. Create 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 test result.

    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 test in the test class.
    all_test = unittest.makeSuite(test_case_class)
  3. Add all_test to test_suite object.
    test_suite.addTest(all_test)
  4. Create unittest.TestResult object.
  5. Run test_suite with the test result object.
  6. Get test result data.

6. Run Test Functions In Multiple Test Class File.

This is similar with above methods. The only difference is that you need to load all the test class 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.

Please see method comments for code explanation.

Below is the project file structures in eclipse PyDev package explorer.

python unittest example file structure

Below is test case execution generated html report file.

python html test runner generated html test report

'''
Created on Aug 19, 2018
@author: zhaosong
'''

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()

Reference

  1. html-testRunner
  2. xmlrunner
(Visited 409 times, 10 visits today)
READ :   Python 3 Matplotlib Draw Point/Line Example

Leave a Reply

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.