This repository contains examples for real-world pytest use cases. It is intended as a "cheatsheet" of working examples that you can pick and choose to meet your particular need.
Resources we really encourage you to learn:
Resource | Description |
---|---|
Pytest home page | The primary resource for pytest knowledge. |
Core mocking framework | This is Python's built-in unittest/mocking framework. It's not easy to read. |
Responses framework | This is a comprehensive mocking framework for HTTP requests. Use it when you want to test a request to a third-party API. |
Moto AWS mocking library | This is a relatively fully featured mocking framework for boto3. It holds AWS resource state in memory, which is especially useful for behavioural tests. |
pip3 install -r requirements.txt
pytest -v
All of the test examples live in the tests folder:
Subfolder | Description |
---|---|
aws | Shows how to mock AWS calls. |
fixtures | Examples for using pytest fixtures. |
http | Shows how to mock HTTP calls - to third party APIs, for example. |
mocking | Examples demonstrating how to mock functions, class methods, etc. |
simple | Demonstrations of common pytest use cases, such as asserting that an exception is thrown. |
Some of the test examples explore classic import gotchas. The source code that is tested resides in lib.
We created a simple "application", fizzbuzz to show many of the testing techniques in action. This application consists of a web server (wrapping a library method in an API) and a client. It demonstrates how you would unit test each part in isolation - so, for example, you would do not run the server to validate the client.
You can find the tests in tests/fizzbuzz.