Pytest - Test Framework¶
Main point
Pytest is a programmer-friendly Python test framework. It makes it easy to write small tests, yet scales to support complex cases.
Major
pytest
features:- Battle-tested and mature.
- Informative test failures.
- Less verbose (plain
assert
vs.self.assertEqual
,self.assertGreaterEqual
, etc.). - Classes are not required.
- A far more convenient way to write setup & teardown functions with fixtures.
- Parameterized tests.
- Fantastic test runner (a.o. marker- and name-based test selection).
- Rich plugin architecture.
- Auto-discovery of test modules and functions.
- Runs
unittest
andnose
test suites out-of-the-box.
You can easily set test runner default flags by defining them in a configuration file called
pytest.ini
. You can find an example in Big-Bang-py.Recommended test runner plugins:
- pytest-cov - prints coverage report at the end of test runner report.
- pytest-mock - adds
mocker
fixture, which makes mocks easier and more readable. - pytest-socket - disables socket calls during tests to ensure network calls are prevented. Amazing to protect yourself against incidental DB or API calls.
- pytest-sugar - makes test runner report even nicer.
Useful content: