PythiloLesson 8 of 10 · progress kept in this browser

Make one test reject several bad implementations

Parametrization runs one test body with several named inputs. The useful measure is not the number of rows, but which incorrect implementations those rows can distinguish. Include different behavioural categories such as zero, positive and negative values.

Here the candidate implementation and the tests are written into a real temporary module. Running that module with pytest proves that a good implementation passes and a plausible wrong one fails. Keep test inputs deterministic, and do not rewrite the expected value using the same algorithm as the candidate: that can reproduce the same mistake on both sides.

import pytest
cases = [(0, 0), (-3, 9), (4, 16)]
@pytest.mark.parametrize(
    "value,expected", cases)
def test_square(value, expected):
    assert value * value == expected
print(len(cases))
Your task

Implement test_square_source(implementation): run a real pytest module combining the supplied source defining square(value) with parametrized checks for (0,0),(-3,9),(4,16). Return pytest exit code as int.

The first run loads pytest into this browser.