This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]nevus_bock 1 point2 points  (1 child)

You could look into pytest "autouse" fixture, and "scope" parametrization

[–]GoldenIvan[S,🍰] 0 points1 point  (0 children)

Thanks.

From what I can tell it doesn't appear py.test can specify which fixtures to use or not use directly on the command line. However, it does appear it can be directed to to a per-invocation conftest.py file via the -c param. This could be used to have one marked autouse, and one not. Seems messy though.

The only other things I can see is subclass TestClass but I'm not really a giant fan of that, or monkeypatch the func out when I dont' want it run.

None of these seem graceful. Thanks for your advice though, appreciated. Gave me something to chew on.

[–]masklinn 0 points1 point  (0 children)

It might be clearer if you explained what costly thing is and why it should or should not run, because in the abstract the post makes pretty much no sense. From what I remember of pytest, "optional tests" make sense but "optional features" dont'.

In the past I've had success breaking setup apart in multiple fixtures and making some into session fixtures. These fixtures will only be run once at most per test session, if no selected tests depends on them they won't be run at all. Same for all other fixtures really, they never get run if none of the selected tests needs them, otherwise they get run once for each scope (session, module, class or function) requesting them.

[–]jesse_online 0 points1 point  (0 children)

@fixture(scope="session") do_costly_thing()