Let's say that you want to test whether or not a year is a leap year for four values [1993, 1992, 1900, 2000]
But instead of testing them all in one file using verify_all
you would like to have 4 seperate approval files.
- test_file.test_name.
1993
.approved.txt - test_file.test_name.
1992
.approved.txt - test_file.test_name.
1900
.approved.txt - test_file.test_name.
2000
.approved.txt
Here is a sample of one of the .approved.
files:
is Leap 1992: True
Here are 3 ways you can do this in a single test.
Approval tests supports parametrized tests, here is an example:
@pytest.mark.parametrize("year", [1993, 1992, 1900, 2000])
def test_scenarios(year: int) -> None:
verify(
f"is Leap {str(year)}: {str(is_leap_year(year))}",
options=NamerFactory.with_parameters(year),
)
Another alternative is to simply make multiple verify()
calls using the NamerFactory.with_parameters
in the code.
Be aware that this will halt your test on the first verify()
that fails, same as a normal assert
.
year = 1992
verify(
f"is Leap {str(year)}: {str(is_leap_year(year))}",
options=NamerFactory.with_parameters(year),
)
year = 1993
verify(
f"is Leap {str(year)}: {str(is_leap_year(year))}",
options=NamerFactory.with_parameters(year),
)
year = 1900
verify(
f"is Leap {str(year)}: {str(is_leap_year(year))}",
options=NamerFactory.with_parameters(year),
)
year = 2000
verify(
f"is Leap {str(year)}: {str(is_leap_year(year))}",
options=NamerFactory.with_parameters(year),
)
To manually verify multiple things without the executation stopping on the first failure,
using gather_all_exceptions_and_throw
will prevent blocking, while still failing on any failure.
Here is an example:
years = [1993, 1992, 1900, 2000]
gather_all_exceptions_and_throw(
years,
lambda y: verify(
f"is Leap {str(y)}: {str(is_leap_year(y))}",
options=NamerFactory.with_parameters(y),
),
)