RSS Amplifier

Rida Ayed · Jul 30, 2025

A minimal example of using namedtuples in pytest parameterized tests

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

What means - in the test parameters below? Nevermind I don’t get it either. Only after counting the commas I could guess gender. @pytest.mark.parametrize ( "lang, salutation, gender, title, expect" , [ ( "DE" , "" , "m" , "" , "doe" , "Hallo" ), ( "DE" , "" , "m" , "Dr." , "doe" , "Hallo Dr. Doe" ), ( "EN" , "" , "-" , "Dr." , "doe" , "Dear Dr. Doe" ), ( "FR" , "" , "f" , "" , "xyz" , "Chère…

What means - in the test parameters below?
Nevermind I don’t get it either.
Only after counting the commas I could guess gender.

@pytest.mark.parametrize(
"lang, salutation, gender, title, expect",
[
    ("DE", "","m","","doe","Hallo"),
    ("DE", "","m","Dr.","doe","Hallo Dr. Doe"),
    ("EN", "","-","Dr.","doe","Dear Dr. Doe"),
    ("FR", "","f","","xyz","Chère Madame Xyz"),
    ("EN", "Dear Bob","m","","doe","Dear Bob"),
])

To put the problem in full context, I’ve simplified and highlighted the parameters in the complete example below:

import pytest

@pytest.mark.parametrize("gender,lang", (
    ('-', 'EN'),
))
def test(gender, lang):
    assert gender

Namedtuples are the salvation for this problem.
Notice now how easy the highlighted gender = '-' is understood.

import pytest
from typing import NamedTuple

class TC(NamedTuple): #TC means TestCase
    gender: str
    lang: str

@pytest.mark.parametrize(
    "tc", (
    TC(gender='-', lang='EN'),
))

def test(tc):
    assert tc.gender
    assert tc.lang

I have described another mitigation for incomprehensive structures here.
Another approach using NamedTuple from typing is shown here youtube.
This blogpost –with a bit more text to glance over– inspired me to write this post.
Finally I settled to the current version of this post upon discovering this article.

Read on /posts/minimal-example-namedtuples-pytest-parameters/

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.