Skip to content

PyTests

Directory

The tests folder should mirror src to ensure that each submodule is properly tested. Include an integration folder under tests to evaluate larger tests.

project
└───src
|   └─── __init__.py
│   └─── ModuleA
│       └─── __init__.py
│       └─── A.py
└───tests
    └─── __init__.py
    └─── ModuleA
         └─── __init__.py
         └─── A_test.py

Warning

It is generally recommended that you do not have tests in your src folder since they would become importable as part of your distribution.

Tip

Tests should include both unit and integration testing.

pytest # run all tests
pytest pytest tests/basic_test.py # run tests in module
pytest tests/basic_test.py::test_hello_world # run a specific test

Info

PyTest provides additional features such as parameter sweeping, fixtures, and logging. These should be applied depending on the test requirements.

Coverage

PyTests offers coverage support. When running PyTests with Coverage, it will generate a HTML that show which parts of your repo have not been traced using PyTests. This lets you know which areas of your code still need to be tested.

pytest --cov=./ --cov-report=html:<OUTPUT_PATH>

Allure

Allure provides a dashboard that aggregates your pytest results. Allure provides additional functionality like tagging for your tests to organize them into a tree structure. In order to use allure, you must install it via Homebrew:

brew install allure

To create and view an Allure report:

pytest --alluredir ./  # run pytests
allure generate --single-file --output ./ # generate a report (HTML)