Context managers instead of fixtures? #9774
-
I've worked on a very large test suite which was made up of likely hundreds of fixtures. This got cumbersome to work with over time because we couldn't do "go to def" on the fixtures in their test function signatures. I'm now looking at setting up a brand new pytest test suite from scratch, and in my team there are thoughts on writing context managers instead of fixtures. The reason is my team is using a mix of neovim and vscode and cannot do a "go to def" on fixtures in test function signatures. I believe it's really only pycharm that has this ability (built-in). Since the intent by pytest is you write fixtures, I'm very hesitant here to break away from that pattern. There might come a day when e.g. pylance/vscode adds support for this "go to def", and then I'll likely regret the choice of going with context managers. But the greatest concern would be if pytest is doing something clever with fixtures, which won't be possible with context managers, such as managing how they are setup/torn down (loaded/unloaded in memory)...?
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
@nicoddemus - what pitfalls might we be looking at if we decide to write our own context managers instead of writing fixtures? |
Beta Was this translation helpful? Give feedback.
-
key items is that you forfeit parameterization integration, scope based test redordering ( to reduce setup/teardown of higher scopes) its generally suggested to use fixtures in pytest, and its indeed unfortunate that IDE integration for the lookup is rather limited |
Beta Was this translation helpful? Give feedback.
-
The most direct benefit of fixtures is probably the easier syntax. Context managers would often lead to indenting all your tests by one level, was well as it being rather awkward to combine multiple context managers. @RonnyPfannschmidt mentioned some more, I can think of some additional ones:
As for the general consensus: I can't speak for everyone obviously, but it seems like a strange thing to do, as you're essentially ignoring what's probably pytest's most central feature/pattern. Note that it's easy to get that functionality in VS Code as well, via a pytest-fixtures plugin. Something similar seems to exist for NeoVim too. FWIW I don't think "built-in" is a relevant criteria really, as with both VS Code and NeoVim you're likely using a (if not many) extensions to use them as Python IDE already. |
Beta Was this translation helpful? Give feedback.
-
Thanks @RonnyPfannschmidt @The-Compiler for your input 👍 Some reflections:
The overall sum of this all is that I think pytest's strengths still outweigh the weaknesses/shortcomings of IDE/editor (plugin) integrations. |
Beta Was this translation helpful? Give feedback.
Thanks @RonnyPfannschmidt @The-Compiler for your input 👍
Some reflections:
The overall sum of this all is that I think pytest's strengths still outweigh the weaknesses/shortcomings of IDE/editor (plugin) integrations.