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. Expensive setup is not repeated either, as long as it lives in a fixture with a scope wider than a single test: a fixture that opens one database connection for the module or the session runs once for all data sets, while a function-scoped fixture opens a new connection for each of them.

Article author: Arthur Isaev