Command Palette

Search for a command to run...

Hectal
Phase 11: Testing and Code Quality
Python · Exercise 2 of 6 PreviousNext
Intermediate

Write pytest tests with a fixture, a table and raises

The program writes the string TEST_CODE to a file and runs pytest on it, then prints how many tests passed and failed. Edit TEST_CODE (the text between the triple quotes) so that it contains real pytest tests for apply_discount.

Add: (1) a fixture named price that returns 200; (2) a test that uses the price fixture to check that a 10 percent discount gives 180.0; (3) a test decorated with @pytest.mark.parametrize with at least three rows of (percent, expected) that uses the price fixture too; (4) a test using pytest.raises(ValueError) that checks a percent of 150 is rejected.

Write your code inside TEST_CODE, under the comment line, with no spaces before def and @. Keep the program's runner code (everything outside TEST_CODE) as it is. All your tests must pass: with these four items you should get at least 5 passing tests (1, plus 3 table rows, plus 1), and the program prints failed: 0.

What Check tests

  1. No test fails
  2. At least 5 tests pass (1 + 3 table rows + 1 raises)
  3. TEST_CODE defines a fixture named price
  4. TEST_CODE uses parametrize with a test that takes price
  5. TEST_CODE uses pytest.raises
  6. The program prints failed: 0

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.