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
- No test fails
- At least 5 tests pass (1 + 3 table rows + 1 raises)
- TEST_CODE defines a fixture named
price - TEST_CODE uses
parametrizewith a test that takesprice - TEST_CODE uses
pytest.raises - 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.
Output
Press Run to see what your program prints.