learning-it by example

Back to the video

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

  1. 0:00 Six tests pass, and one catches float64 where int64 was expected
  2. 0:32 The idea
  3. 1:02 Setup
  4. 1:24 The function under test
  5. 1:52 A fixture in conftest
  6. 2:11 Expected frame and parametrize
  7. 2:40 Rules on a thousand rows
  8. 3:26 The naive version
  9. 4:10 What this does not show
  10. 4:33 Mistakes to avoid
  11. 5:05 My take and next steps
  12. 5:32 Recap

Files

  • clean.py (17 lines)
  • conftest.py (13 lines)
  • test_clean.py (41 lines)
  • test_naive.py (9 lines)

Download the code (zip)

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

Learning IT by Example.