2026-10-09 — Getting started with pytest¶
Resource watched¶
Title: Getting started with pytest
Type: Video
Summary and takeaways¶
Testing is crucial for validating code, making it reliable, reusable and maintainable.
pytest is a popular, easy-to-use testing framework for Python that is replacing (phasing out) built-in unittest module.
pytest has a lot of features. Some key features to know/learn about (most of them are briefly discussed in the video):
-
test functions and test classes (writing test functions is the colloquially preferred option)
-
fixturres (see this YouTube video or pytest docs)
-
patching (see pytest docs, but look into other resources as well because pytest built-ins, especially
Monkeypatchare not always the most robust solutions) -
coverage reports (see Python Developers Guide). CodeCov is a popular resource that can be used to generate reports and badges for public repositories.
-
random execution using
pytest-randomly -
there are many paradigms and strategies in testing (unit tests, integration tests etc), there is polenty to explore.
What should I test and how?
-
Test the key pieces that are central algorithm and workflow. E.g. if you are writing a module that calculates SASA test that it produces correct results. On the other hand you may want to test that your CLI workflow is running correctly depending on the input options.
-
Test for errors and edge cases! E.g. incorrect inputs, inputs producing numerical instability etc.
-
Look for examples in other scientific packages. E.g. this is a test suite for MDAnalysis; it's huge, so you can inspect overall structure and maybe small code details for helpful examples.
-
Also take a look beyound scientific packages, because those can lag behind best practices established in industry. There are also general test samples like this one.
Resources¶
- pytest documentation
- antonywritescode channel has a lot of videos dedicated to pytest and testing more generally. E.g.
- Another interesting library:
Hypopthesis, property-based testing library for Python.