Unit testing, code coverage and mocking with Pytest
, and property-based testing with Hypothesis
.
https://github.com/4OH4/pytesting
This repository is the companion code for a forthcoming blog post on Python software testing. Feedback encouraged - please raise an issue.
Clone the repository, install requirements into your Python environment and run the tests:
git clone https://github.com/4OH4/pytesting
cd pytesting
pip install -r requirements.txt
python run_tests.py
Clone the repository and run everything inside a Docker container:
git clone https://github.com/4OH4/pytesting
cd pytesting/Docker
docker-compose -d up
docker-compose run pytesting
python run_tests.py
Python 3 and the following packages (all available via pip
):
- pytest
- pytest-cov
- pytest-mock
- pytruth
- hypothesis
Or install via the requirements.txt
file:
pip install -r requirements.txt
Tests should be run from the command line, in the repository root.
Doctests are executed for a single module at a time. The command runs the tests in database.py (for example):
python -m doctest -v src/database.py
The -v
argument requests verbose output - otherwise it only reports test failures.
To run all test cases:
pytest
To run the code coverage report:
pytest --cov=src tests