Skip to main content

A small unittest-based tool for generating single page html reports in Python.

Project description

:page_facing_up: unitreport PyPI

UnitReport 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.

unitreport

Getting Started

You can install the library using,

pip3 install unitreport

There are usage examples in test_plots.py and test_tables.py. You need to create test cases using the unittest Python library, and utilise unitreport's decorators:

import unittest
import unitreport

import seaborn as sns
import pandas as pd

class TestExample(unittest.TestCase):
    """Example test suite producing plots and tables in a report using unitreport."""

    dataset: pd.DataFrame = None

    @classmethod
    def setUpClass(cls):
        """Load dataset on setup."""
        cls.dataset = sns.load_dataset("fmri")

    @unitreport.plotting
    def test_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 plotting
        self.assertEqual(self.dataset.shape, (1064, 5))

        # plotting decorator will call plt.savefig() to generate the plot
        sns.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.tabling
    def test_region_counts(self):
        """*fMRI data:* table summary description of timepoints and signals."""
        # you can still run assertions to check data quality before making table
        self.assertEqual(self.dataset.shape, (1064, 5))

        return self.dataset.describe().to_html()

You can run the tests using,

python3 -m unitreport

This 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 template index.html and main.css files 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 receive date (today's date), and figures, a dictionary with test function names as keys mapped to values of type (table or plot), content (svg or html table) and description (test function's docstring).

You can also invoke unitreport from a Python script using the library's main() (runs tests and generates report), discover_and_run() (only run tests) and generate_report() (only generate report) functions. These utilise the default values for the above parameters if not specified by the user, and generate_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.

import unitreport

# result is a unittest.TestResult which you can access things such as result.errors
result, 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 report
html_report = unitreport.generate_report()

# same as above, raises assertion error if there are errors in tests or no tests found
result, figs, html_report = unitreport.main()

Built With

  • unittest - underlying testing framework
  • Jinja2 - rendering and generating the report
  • matplotlib - plotting library
  • Markdown - Python markdown library for markdown captions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unitreport-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unitreport-0.1.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file unitreport-0.1.1.tar.gz.

File metadata

  • Download URL: unitreport-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for unitreport-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b19826e09c04527467ecedbb5d3053bf52d7c24debb1f4ded603138f92c5d99a
MD5 144ae26eadcb00925326288fb2ef1b7a
BLAKE2b-256 c247aabb7395e82da94754b6152202491fa9a3f61fba49241d8380775ae7580b

See more details on using hashes here.

File details

Details for the file unitreport-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: unitreport-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for unitreport-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4d5ec32393fb26603791962b10a2a301ad852798f1a992e882ed6f10af519455
MD5 26d572ac84722233807a5dfa8a3be0b5
BLAKE2b-256 1c28643735e43141bcca9c6030fef867b23e1c333a7a46ac6a45dfe9e6aa4b4e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page