Block 137: Testing with pytest
Write and run automated tests for your Python functions.
Concepts
- pip install pytest
- Writing test functions: def test_*
- assert statements in tests
- Running tests: pytest from command line
- Fixtures for setup and teardown
Code Examples
See exercise below.
Exercise
Write 5 tests for your compute_bmi() function (normal cases, edge cases, invalid input). Create a conftest.py fixture that provides a sample DataFrame for multiple tests.
Homework
Why should you write tests BEFORE you think your code is working? What is TDD?