Parametrized Test

noun

say it: “puh-RAM-ih-tryzd TEST” /pəˈræmətraɪzd tɛst/

One test that runs several times, each time with a different set of inputs and the answer expected, instead of a copy of the test for each case.

What is a parametrized test?

A parametrized test runs the same test with several sets of inputs and expected answers, instead of copying the test over and over.

import pytest

def shipping(total):
    return 0 if total >= 50 else 5

@pytest.mark.parametrize("total, expected", [(20, 5), (50, 0), (80, 0)])
def test_shipping(total, expected):
    assert shipping(total) == expected

In a sentence

  • “pytest reports each case of a parametrized test as its own pass or fail.”
  • “A parametrized test checked shipping for totals of 20, 50 and 80 in one function.”

Level: Freelancer · More Python terms at this level

Updated