Test parametrization in plain words

Introduction

When several tests check the same function with different arguments, the result looks clumsy:

Test 1 Check that Add(0, 0) returns 0 Test 2 Check that Add(-1, 1) returns 0 … Test 9 Check that Add(7, 8) returns 15

Instead of running the same test with different input, it is more convenient to write one test that takes several sets of arguments.

That is what parametrization usually means.

How it looks

Roughly like this:

ParametrizedTest((0, 0, 0), (-1, 1, 0) …, (7, 8, 15))

One point to avoid confusion: a parametrized test does not glue the tests into one big test. If there were nine tests, nine remain, and they pass or fail independently of each other.

The idea is the same in every tool, only the syntax differs. In pytest the decorator with the list of tuples does the job, and the page pytest parametrize shows the details. In Robot Framework the same result comes from a test template over a list of arguments, see parametrization in Robot Framework .

Why it pays off

Besides shorter code, parametrization has a few advantages.

Such a test is easier to scale: one more set of data is one more line, not a copied function. Heavy steps are not repeated either, which matters for performance.

For example, it is far more efficient to connect to a database at the beginning of the test and close the connection at the end than to open and close it for every data set.

Article author: Arthur Isaev