learning-it by example

#146 Python for data 6 min

Test Data Code with pytest: Fixtures and Frames

Six pytest tests pass, and one catches a column that came back float64 instead of int64.

1. In the pytest video for a pandas cleaning function, six tests passed and one test written against a naive version failed. What was the failure?
2. Why does a pandas whole-number column with a gap turn into floats, and does fillna(0) alone turn it back?
3. Where does the video put the fixture that builds four made-up orders, so that more than one test file can use it?
4. The test that tries four spellings of north edits row zero of the fixture. Why is that safe?
5. What does pandas assert_frame_equal check by default, according to the video?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Predict the failing line

    Copy the code folder first. Read clean.py and test_naive.py without running them. Predict which column and which attribute the failing test reports, and what the left and right values will be. Then, with pandas, NumPy and pytest installed as in the video, run python -m pytest -qq --tb=short test_naive.py and compare. The video's run reported column qty with dtype float64 on the left and int64 on the right.

    A hint

    The values 2, 0, 0, 5 are right in both frames. Only one attribute differs.

  2. Fix the naive fill

    In clean.py change fill_qty_naive so that it ends with .astype("int64") after fillna(0), the same last step that clean_orders uses. Predict whether test_naive.py now passes. Then run python -m pytest -qq test_naive.py to check.

    A hint

    The only difference between the naive fill and the real one is the type step.

  3. Add a fifth spelling

    In test_clean.py add one more spelling to the parametrize list, for example "North " or " nOrth". Predict how many tests pytest reports for test_clean.py, given that the run in the video showed six. Then run python -m pytest -qq test_clean.py to check.

    A hint

    Each spelling counts as its own test.

  4. Break a rule on purpose

    In test_clean.py change the rule set(out["region"]) == {"North", "South", "East"} so the set also contains "West". Predict which test fails, and whether the small example test still passes. Then run python -m pytest -qq test_clean.py to check.

    A hint

    The small example test does not touch the thousand-row fixture.

These tasks are for you to try on your own machine. Nothing is checked or saved.

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.

Up next in Python for data

More in Python for data

All Python for data videos

Related videos