This repository is a study-focused collection of examples, exercises, and theory on Unit Testing and Automated Testing in Python.
It contains the examples and cases proposed in the tutorial "Please Learn How To Write Tests in Python… • Pytest Tutorial" on Tech With Tim, demonstrating practical usage of pytest, mocking functions and classes, and Flask API testing.
It contains practical examples covering:
- Writing unit tests for functions and classes
- Using pytest fixtures for setup and teardown
- Mocking functions and classes with
mocker
- Parametrized testing
- Testing Flask API endpoints
- Conceptual explanations of unit tests, integration tests, system tests, and automated testing
- Practical examples demonstrating best practices for Python testing
The goal of this repo is to serve as a learning resource for Python developers who want to deepen their understanding of testing, from basics to intermediate concepts.
api.py -> Simulated Flask API for testing
test_api.py -> Tests for the Flask API
Python-Testing/
├── fixtures-setup-example/
│ ├── main.py -> Example functions to be tested
│ └── test_main.py -> Tests using setup fixtures
├── fixtures-teardown/
│ ├── db.py -> Simulated database module
│ └── test_db.py -> Tests using teardown fixtures
├── mocks-classes/
│ ├── service.py -> Example class to be tested
│ └── test_service.py -> Tests mocking classes
├── mocks-functions/
│ ├── db.py -> Example function to be tested
│ ├── main.py -> Example functions to be tested
│ ├── test_db.py -> Tests mocking functions
│ └── test_main.py -> Tests for main.py
├── parametrized-testing/
│ ├── main.py -> Example parametrized functions to be tested
│ └── test_main.py -> Parametrized tests
├── theory/
│ ├── fixtures.pdf -> Explanation of pytest fixtures
│ ├── mocking.pdf -> Explanation of mocking in tests
│ ├── testing_types.pdf -> Overview of different test types
│ └── unit_testing.pdf -> General unit testing theory
└── simple-functions-example/
├── main.py -> Simple functions to be tested
└── test_main.py -> Tests for simple functions
requirements.txt -> Project dependencies
README.md -> This file
- Fixtures: setup and teardown examples for database and object lifecycle management.
- Mocks: clear comparison between mocking functions vs classes.
- Parametrized Tests: reducing repetition and testing multiple cases efficiently.
- Flask API Testing: demonstrating testing of endpoints with
client
fixtures. - Documentation: PDFs explaining testing concepts, fixtures, mocking, and general theory.
pip install requirements.txt
pytest
- All test files follow the pattern test_*.py.
- Use pytest --maxfail=1 --disable-warnings -q for concise output.
- Learn how to write clean unit tests for functions and classes.
- Understand how to use pytest fixtures for test setup and teardown.
- Learn when and how to mock functions vs classes.
- Learn best practices for parametrized testing to avoid repetition.
- Learn how to test APIs built with Flask.
- Understand the theory behind unit, integration, system, and end-to-end testing.
- Understand how automated tests fit into CI/CD pipelines.
Python 3.13 pytest 8.4.2 Flask 2.3.2 mocker (pytest-mock) 3.10.0