Injecting Pytest fixtures without cluttering test signatures

Redowan Delowar December 2, 2024
Source

Sometimes, when writing tests in Pytest, I find myself using fixtures that the test function/method doesn't directly reference. Instead, Pytest runs the fixture, and the test function implicitly leverages its side effects. For example:

In the test_stuff function above, we directly use the mock_svc fixture but not mock_env. Instead, we expect Pytest to run mock_env, which modifies the environment variables. This works, but IDEs often mark mock_env as an unused parameter and dims it out.

One way to avoid this is by marking the mock_env fixture with @pytest.fixture(autouse=True) and omitting it from the test function's parameters. However, I prefer not to use autouse=True because it can make reasoning about tests harder.

TIL that you can use @pytest.mark.usefixtures to inject these implicit fixtures without cluttering the test function signature or using autouse. Here's the same test marked with usefixtures:

Now, the mock_env fixture is applied without cluttering the test function's signature, and no more greyed-out unused parameter warnings! The usefixtures marker also accepts multiple fixtures as variadic arguments: @pytest.mark.usefixtures("fixture_a", "fixture_b").

One thing to keep in mind is that it won't work if you try to mark another fixture with the usefixtures decorator. The pytest documentation includes a warning about this.

Fin!

Discussion in the ATmosphere

Loading comments...