TIL how to use namedtuple with pytest parameterized tests to make the parameters easier to read https://til.simonwillison.net/pytest/namedtuple-parameterized-tests
-
TIL how to use namedtuple with pytest parameterized tests to make the parameters easier to read https://til.simonwillison.net/pytest/namedtuple-parameterized-tests
-
@simon I wonder if dataclasses could be used to help with type safety. (I don't see why not. )
-
@simon also,nis there a reason to prefer splatting the named tuple fields, instead of one argument which is the tuple/dataclass itself?
-
@thejcannon not sure what you mean there, can you give a code example?
-
@simon Yeah sure thing!
```py
@pytest.mark.parametrize(
"param",
[ManageTableTest(...), ...],
)
def test_manage_table_permissions(param: ManageTableTest):
assert param.description
```It avoids repeating the fields and avoids potential name collisions with fixtures.
(which is why dataclasses seem like a good next step, because we really didn't gain much from the `NamedTuple` anymore)
-
@thejcannon oh I see! Yeah I can’t see any reason not to do that - I’ve been using parametrize with multiple arguments for so long it didn’t cross my mind to bundle everything into one like that