Skip to content

Latest commit

 

History

History
111 lines (85 loc) · 3.87 KB

multiple_approvals_per_test.md

File metadata and controls

111 lines (85 loc) · 3.87 KB

How to approve multiple files in one test

Contents

The Scenario

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.

  1. test_file.test_name.1993.approved.txt
  2. test_file.test_name.1992.approved.txt
  3. test_file.test_name.1900.approved.txt
  4. test_file.test_name.2000.approved.txt

Here is a sample of one of the .approved. files:

is Leap 1992: True

snippet source | anchor

Here are 3 ways you can do this in a single test.

Method 1: Verifying Parametrized Tests

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),
    )

snippet source | anchor

Method 2: Verify Multiple Things with Blocking

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),
)

snippet source | anchor

Method 3: Verify Multiple Things without Blocking

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),
    ),
)

snippet source | anchor