05-10-2021

Test

' A TestRunner for use with the Python unit testing framework. It generates a HTML report to show the result at a glance. The simplest way to use this is to invoke its main method. Import unittest import HTMLTestRunner. Define your tests. Here is a stripped down real-life example of using parametrized testing for testing serialization of objects between different python interpreters. We define a testbasicobjects function which is to be run with different sets of arguments for its three arguments: python1: first python interpreter, run to pickle-dump an object to a file. Run tests that failed in the most recent test run. Similar to Python: Run Failed Tests on versions prior to 2021.9. Test: Rerun Last Run: Debug tests that were executed in the most recent test run. Test: Run All Tests: Run all discovered tests. Equivalent to Python: Run All Tests on versions prior to 2021.9. Test: Run Test at Cursor. If you want to run specifically that test function (in this case, testanswer), right click anywhere in the function that you want to test and choose Run 'pytest for (the file name).(the function name)' - so in this case, sample.testanswer. You, similarly, can put your line.

HtmlTest runner is a unittest test runner that saves results in a human-readable HTML format.

This Package was inspired by unittest-xml-reporting andHtmlTestRunner by tungwaiyip and began by combining the methodology of the former with the functionality of the latter.

Table of Content

Installation

To install HtmlTestRunner, run this command in your terminal:

This is the preferred method to install HtmlTestRunner, as it will always install the most recent stable release.If you don't have pip installed, this Python installation guide can guideyou through the process.

Usage:

With unittest.main()

Just import HtmlTestRunner from package, then pass it to unittest.main with the testRunner keyword.Tests will be saved under a reports/ directory by default (the output kwarg controls this.).

With Test Suites

HtmlTestRunner can also be used with test suites; just create a runner instance and call the run method with your suite.Here an example:

Combining Reports into a Single Report

By default, separate reports will be produced for each TestCase.The combine_reports boolean kwarg can be used to tell HTMLTestRunner to instead produce a single report:

Python Scripts For Test Automation

Python

Setting a filename

By default the name of the HTML file(s) produced will be created by joining the names of each test case together.The report_name kwarg can be used to specify a custom filename.For example, the following will produce a report file called 'MyReport.html':

Console output:

This is an example of the console output expected when using HTMLTestRunner.

Unittest Htmltestrunner

Test Result:

This is a sample of the results from the template that came by default with the runner.

Custom Templates:

If you want to use your own template you can pass the absolute path when instantiating the HTMLTestRunner class using the template kwarg:

Your template must use jinja2 syntax, since this is the engine we use.

When using any template, the following variables will be available by default for use by jinja2:

Html
  • title: This is the report title - by default this is 'Unittests Results' but can be changed using the report_title kwarg
  • headers: This is a dict with 2 items:
    • start_time: A datetime object representing when the test was run
    • status: A dict of of the same form as the sub-dicts described below for summaries but for all tests combined
  • all_results: A dict - keys are the names of each test case and values are lists containing test result objects (see the source code or the template for what information these provide)
  • summaries: A dict - keys are the names of each test case and values are dicts containing:
    • total: The total number of tests
    • success: The number of passed tests
    • failure: The number of failed tests
    • error: The number of errored tests
    • skip: The number of skipped tests
    • duration: A string showing how long all these tests took to run in either seconds or milliseconds
Html

Furthermore, you can provide any number of further variables to access from the template using the template_args kwarg.For example, if you wanted to have the name of the logged in user available to insert into reports that could be achieved as follows:

Now the user name can be accessed from a template using jinja2 syntax: {{ user }}.

Unittest htmltestrunner

Html Test Runner Python Code

Click here for a template example, this is the default one shipped with the package.

TODO

  • Add Test
  • Improve documentation
  • Add custom templates
  • Add xml results
  • Add support for Python2.7
  • Add support for one report when running test suites.

Contributing

Python Automation Tutorial

Contributions are welcome, and they are greatly appreciated! Everylittle bit helps, and credit will always be given.

For more info please click here

Html Test Runner Python Online

Credits

Python Html Test Runner

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.