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"]:
    ...
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.