4.1 Pytest Framework
pytest is a mature full-featured Python testing tool that helps you write better programs. It is widely preferred over the built-in unittest module due to its simple syntax and powerful features.
4.2 Fixtures and Mocking
Fixtures: Functions that run before (and sometimes after) tests to set up state (e.g., database connections).
Mocking: Replacing parts of your system under test with mock objects and making assertions about how they have been used.
4.3 Type Hinting (Mypy)
Python is dynamically typed, but type hints allow you to check your code for type errors before running it. mypy is a static type checker for Python.
def greeting(name: str) -> str:
return 'Hello ' + name
4.4 Linting and Formatting
Tools like Black (formatter) and Flake8 or Ruff (linters) help maintain code quality and consistency across a team.
🎯 Practical Exercise
Write a unit test for a calculator function using pytest. Use a fixture to provide sample data.