Python code examples for Unit Testing
The examples are written to reflect how a simple OrderValidator class which doesn't follow Single Responsibility Principle isn't unit testable can be split to multiple testable classes that following that principle and are unit testable.
- The not testable examples are under folders "not_testable"
- The testable examples are under folders "testable"
The test code uses:
- Clone this repo
- Install Python 3.10 if not already installed
- Install Python poetry dependency management tool: https://python-poetry.org/docs/
- Open a terminal (cmd/bash/PowerShell/etc.)
- Recommended: configure poetry to create virtual environments in project folder:
poetry config virtualenvs.in-project true - Navigate to repo root directory
- Install dependencies using poetry:
poetry install - Activate the virtual environment:
poetry shell - Run the tests:
poetry run pytest