Skip to content
Mateus Oliveira edited this page Sep 7, 2022 · 9 revisions

Tests assure the code does what it is supposed to do.

The template enforces testing in two ways:

  • pytest: to write and execute tests and enforce all tests executed were successfully.
  • pytest-cov: a plugin for pytest to write coverage report and enforce 100% coverage.

Remember that 100% coverage does not mean that the code is fully tested. Example

def divide(dividend, divisor):
    return dividend / divisor

def test_divide():
    assert divide(2,1) == 2

Code coverage would tell us the function divide is fully covered, but if input the divisor as zero, a ZeroDivisionError exception would be triggered. Or if we change the operator from division to multiplication, our test would still pass. Testing boundaries and different inputs can help you have tests more reliable.

Configuration

https://github.com/mateusoliveira43/python-project-template/blob/main/pyproject.toml#L35-L44

Running

The command pytest will run the project tests and measure and write the coverage report.

pytest successful run

pytest failure run

To only run the tests, without measuring code coverage, run pytest --no-cov.

A good idea is to organize your different tests in folders, like unit, integration and functional. Then, to run only the unit tests, run pytest tests/unit.

Clone this wiki locally