Pytest - Parameterizing Tests


Python clean test tip:

Use pytest parametrize when you need multiple cases to prove a single behavior.

Example:

import difflib
import pytest


def names_are_almost_equal(first, second):
    return difflib.SequenceMatcher(None, first, second).ratio() > 0.7


@pytest.mark.parametrize(
    "first,second",
    [
        ("John", "Johny"),
        ("Many", "Mary"),
    ]
)
def test_names_are_almost_equal(first, second):
    assert names_are_almost_equal(first, second)


@pytest.mark.parametrize(
    "first,second",
    [
        ("John", "Joe"),
        ("Daisy", "Serena"),
    ]
)
def test_names_are_not_almost_equal(first, second):
    assert not names_are_almost_equal(first, second)