Test Data Code with pytest: Fixtures and Frames
Six pytest tests pass, and one catches a column that came back float64 instead of int64.
Video #146 ยท Python for data
Steps in the video
- 0:00 Six tests pass, and one catches float64 where int64 was expected
- 0:32 The idea
- 1:02 Setup
- 1:24 The function under test
- 1:52 A fixture in conftest
- 2:11 Expected frame and parametrize
- 2:40 Rules on a thousand rows
- 3:26 The naive version
- 4:10 What this does not show
- 4:33 Mistakes to avoid
- 5:05 My take and next steps
- 5:32 Recap
Files
- clean.py (17 lines)
- conftest.py (13 lines)
- test_clean.py (41 lines)
- test_naive.py (9 lines)
Tested with
Python 3.12.15 | numpy==2.5.4, pandas==3.1.0, pytest==9.1.1
What this does not show
Made-up data and a tiny function; the failing test shows a bug set up on purpose, not one found in real work.
Sources
- https://docs.pytest.org/en/stable/how-to/fixtures.html
- https://docs.pytest.org/en/stable/how-to/output.html
- https://docs.pytest.org/en/stable/how-to/parametrize.html
- https://pandas.pydata.org/docs/reference/api/pandas.testing.assert_frame_equal.html
- https://pandas.pydata.org/docs/user_guide/missing_data.html
- https://pypi.org/pypi/numpy/json
- https://pypi.org/pypi/pandas/json
- https://pypi.org/pypi/pytest/json
Learning IT by Example.