Package: unitreport
:page_facing_up: unitreportUnitReport is a small unittest-based tool for generating single page html reports in Python.
The reports can include matplotlib figures and html tables.
It is designed to be minimal and fit into the unittesting framework, allowing users to make assertions about their data (e.g. data quality) before generating figures.Getting StartedYou can install the library using,pip3 install unitreportThere are usage examples intest_plots.pyandtest_tables.py.
You need to create test cases using the unittest Python library, and utilise unitreport's decorators:importunittestimportunitreportimportseabornassnsimportpandasaspdclassTestExample(unittest.TestCase):"""Example test suite producing plots and tables in a report using unitreport."""dataset:pd.DataFrame=None@classmethoddefsetUpClass(cls):"""Load dataset on setup."""cls.dataset=sns.load_dataset("fmri")@unitreport.plottingdeftest_timepoint_vs_signal_by_event(self):"""*fMRI data:* timepoint versus signal for stim and cue events."""# you can still run assertions to check data quality before plottingself.assertEqual(self.dataset.shape,(1064,5))# plotting decorator will call plt.savefig() to generate the plotsns.relplot(x="timepoint",y="signal",hue="event",style="event",kind="line",data=self.dataset,)# could also return a figure & .savefig() will be called on returned object@unitreport.tablingdeftest_region_counts(self):"""*fMRI data:* table summary description of timepoints and signals."""# you can still run assertions to check data quality before making tableself.assertEqual(self.dataset.shape,(1064,5))returnself.dataset.describe().to_html()You can run the tests using,python3 -m unitreportThis will discover and run the tests (which could be across multiple test files), generate the report and save it to the current directory.For extra parameters you can run the following,python3 -m unitreport -h

usage: __main__.py [-h] [--tests_dir TESTS_DIR] [--pattern PATTERN]
                   [--templates_dir TEMPLATES_DIR] [--output_file OUTPUT_FILE]

optional arguments:
  -h, --help            show this help message and exit
  --tests_dir TESTS_DIR
                        Path to test files. (default: .)
  --pattern PATTERN     File patterns to discover test cases in. (default:
                        test*.py)
  --templates_dir TEMPLATES_DIR
                        Path to jinja2 templates directory including
                        index.html and main.css. (default: (unitreport) templates)
  --output_file OUTPUT_FILE
                        Output path including name. (default: report.html)There are templateindex.htmlandmain.cssfiles which will be used by default to generate the style of the report.
You can also specify a path to your own templates using--templates_dir, where the html Jinja2 template can expect to receivedate(today's date), andfigures, a dictionary with test function names as keys mapped to values oftype(table or plot),content(svg or html table) anddescription(test function's docstring).You can also invoke unitreport from a Python script using the library'smain()(runs tests and generates report),discover_and_run()(only run tests) andgenerate_report()(only generate report) functions.
These utilise the default values for the above parameters if not specified by the user, andgenerate_report()uses the global FIGURES dictionary generated from the tests if not passed. For more details on what they do, you can check the source code.importunitreport# result is a unittest.TestResult which you can access things such as result.errorsresult,figs=unitreport.discover_and_run()print(result)# <unittest.runner.TextTestResult run=3 errors=0 failures=0>print(figs)# Dict with test function names as keys mapped to 'type', 'content', and 'description'# html_report is a string containing the generated reporthtml_report=unitreport.generate_report()# same as above, raises assertion error if there are errors in tests or no tests foundresult,figs,html_report=unitreport.main()Built Withunittest- underlying testing frameworkJinja2- rendering and generating the reportmatplotlib- plotting libraryMarkdown- Python markdown library for markdown captions