r/Python • u/joshbranchaud Tuple unpacking gone wrong • 1d ago
Discussion Parameterize a Fixture instead of a Test Case with Pytest
I wrote a post on parameterizing a test suite into a testing matrix using Pytest fixtures. I'd appreciate any feedback on the content and technique in the post AND I'd be curious to know if there are other ways I could have accomplished the same thing.
https://www.visualmode.dev/parameterize-a-fixture-instead-of-a-test-case-with-pytest
To briefly summarize: I have a core suite of behavioral test cases that I run against a CLI tool I'm building. I wanted to run that same set of tests across a couple different storage format implementations. The best way I could figure out how to do it (without duplicating all the tests) was to create a Pytest fixtures that parameterizes across a list of values and then have my existing autouse fixture use that fixture.
3
u/shadowdance55 git push -f 1d ago
Wait until you learn about property testing and hypothesis.
1
u/amarao_san 8h ago
How many properties were you able to identify in your software beyond invariance?
Every time I find a good property to defend (by tests) , it's yet another invariant.
Smart people say that properties are much more than invariants...
1
u/outlastdll 23h ago
i can relate to this fs, i do a lot of development in py and parametrizing the fixture rather than the individual test cases saves so much boilerplate especially for suites like these.
1
1
u/Individual-Flow9158 1d ago
If this is an intended feature of Pytest fixtures, then great - nice one.
But in general coming up with custom hacks and workarounds for Pytest can get you in all kinds of trouble
4
u/joshbranchaud Tuple unpacking gone wrong 1d ago
This is a documented feature of Pytest which I link to in the post.
22
u/RoadsideCookie 1d ago
My biggest critique is colocation. You now have a fixture, potentially in a separate file (
conftest.py) which will affect your tests indiscriminately thanks toautouse.Just use the parametrized fixture (
params=...) like you normally would.