This lesson is part of the Network Testing with Pytest course.
In this lesson, you will learn:
- What is test parametrization?
- How to refactor our Scrapli example to use test parametrization.
You can find the scripts and code for this lesson within the Pytest repo under the directory: 004_parametrization/002_test_parametrization.
What is Test Parametrization?
Rather than create a fixture with multiple values and then pass them all into the test at once, we can parametrize these values by supplying them directly to the test like so:
@pytest.mark.parametrize("expected_vlan", ["100", "101", "102"])
def test_vlan(expected_vlan, device_vlans):
assert expected_vlan in device_vlans
Here we supply the parameters via the pytest.mark.parametrize
decorator. The first value within our decorator is the parameter name. We then supply a list of the parameters. You can think of the parameter name and the list of parameters a little like a for loop (below). Pytest will iterate through the parameters, creating a new test for each value within the parameter list.
for expected_vlan in ["100", "101", "102"]:
...