Unit testing with the pytest module
The pytest
tool allows us to step beyond the examples used by doctest
. Instead of using a subclass of unittest.TestCase
, the pytest
tool lets us use function definitions. The pytest
approach uses Python's built-in assert
statement, leaving the test case looking somewhat simpler. The pytest
test design avoids using the complex mix of assertion methods.
The pytest
tool is not part of Python; it needs to be installed separately. Use a command like this:
python -m pip install pytest
In this recipe, we'll look at how we can use pytest
to simplify our test cases.
Getting ready
The ideas behind the Gherkin language can help to structure a test. In this test specification language, each scenario is described by GIVEN-WHEN-THEN steps. For this recipe, we have a scenario like this:
Scenario: Summary object can add values and compute statistics.
Given a Summary object
And numbers in the range 0 to 1000 (inclusive...