Skip to content
CodeKruPro

Drive one pytest test from a data file with parametrize and a fixture

Automation taskMediumPython onlyAsked in: Accenture, Capgemini, Cognizant, Infosys, SaaS Labs, Swiggy, TCS, Wipro

Interviewers ask: "How do you run the same test with many data sets?" and "What are fixtures and parametrize?" In this task you turn three hand-written tests into one test that pytest runs once per row of a data file, with the client (the object your tests use to call the service) supplied by a fixture. The example comes from an AI-testing course, and the pytest skills are the same ones framework rounds check.

pytest parametrize, fixtures and markers for evals

Module · Python and LLM APIs bridge

beginner

about 25 min

What this lesson teaches

The idea

Keep test cases in a data file, one per line, and let pytest run one test function once for each row.

Why a tester cares

Anyone can add a case, often one per reported bug, without writing new code, and a failure names the exact case that broke.

Words you will meet

pytest:
the most common Python test runner.
Parametrize:
run one test once per row of data.
Fixture:
setup code, such as creating an API client, that pytest hands to every test that asks for it.
JSONL:
a file with one JSON object on each line.

What you will do

  1. 1Read the data file
  2. 2One parametrized test
  3. 3Submit
Jump to practice

An eval suite grows one case at a time: every bug a customer reports becomes a new row. If each row needs a new test function, nobody adds rows. The usual fix is to keep cases in a data file and drive one test from it.

cases.jsonl holds one JSON object per line: an id, a question, phrases the reply must_contain, and phrases it must_not_contain.

Your task

  1. Replace the three hand-written tests with one test that pytest runs once per row of cases.jsonl (@pytest.mark.parametrize), using each row's id as the test id.
  2. Give the test the API client through a pytest fixture called client.
  3. Check every phrase in both lists, ignoring upper and lower case.

On submit, the file may contain rows you have not seen. Your test must pick them up without any change to the code.

Sign in to practise

The lesson is free to read. A free account lets you talk to the practice bots, run your code and save your score. You will come back to this lesson.

1

Your code

against the practice bot

How this is checked: When you submit, your tests run against 4 versions of RefundBot: 2 correct and 2 with a planted fault. Good tests pass on every correct version and fail on every faulty one. Run tries them on the practice copy first; the result table shows each version.

Core checks earn 50 points and the harder checks the other 50. You can submit as many times as you like.