Interview Prep/Python for DevOps/Testing, services & metrics

How does `@pytest.mark.parametrize` work?

BeginnerConceptTesting, services & metrics
Answer

It runs one test function with multiple input sets, each reported as its own test.

@pytest.mark.parametrize("cidr,port,expected", [
    ("0.0.0.0/0", 22, True),
    ("10.0.0.0/8", 22, False),
    ("0.0.0.0/0", 443, False),
    ("::/0", 3389, True),
], ids=["ssh-world", "ssh-private", "https-world", "rdp-ipv6"])
def test_is_risky(cidr, port, expected):
    assert is_risky(cidr, port) is expected

Edge cases become one line each, which is exactly how you should test rule engines like security-group audits.

All Python for DevOps questions Roadmap: Python for automation

More on Testing, services & metrics