Code for pluralsight course Unit Testing with Python by Emily Bache
- Stub - returns a hard coded answer to any query (no logic)
- Fake - is a real implementation, yet unsuitable for production
- Mock - is as a Stub, but additionally verifies interactions
- Test Spy - lets you query afterwards to find out what happened
- Dummy - is for when the interface requires an argument
- Monkeypatching - changing code at runtime
- Unit Testing with Python - Basic Example Using unittest
- Why and When Should You Write Unit Tests
- Using Pytest for Unit Testing in Python
- Testable Documentation with Doctest
- Test Doubles: Mocks, Fakes and Stubs
- Test Coverage and Parameterized Tests
python -m unittest -v
python -m pytest --doctest-modules -v
Should be in proper folder:
python -m doctest test_* -v
coverage
andpytest-cov
packages are required- Add
pragma: no cover
to exclude code from coverage report
Terminal report:
python -m pytest --cov-report term-missing --cov <target>
HTML report:
python -m pytest --cov-report html --cov <target>
To generate report:
python -m coverage run -m unittest
To view report in terminal:
python -m coverage report
To view report in HTML:
python -m coverage html
- Create
.coveragerc
- Add - Add following code to it
[run]
branch=True