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.
More on Testing, services & metrics
Why should automation scripts have tests at all?BeginnerHow does pytest discover and run tests?BeginnerWhy does pytest use plain `assert` instead of `assertEqual`?BeginnerWhat are pytest fixtures, and what scopes exist?IntermediateWhat built-in fixtures do you use most?IntermediateWhat is moto, and how do you use it with pytest?Intermediate