Can pytest hooks use fixtures?
See original GitHub issueI know fixtures can use other fixtures, but can a hook use a fixture? I searched a lot on net but could not get any help. Can someone please point if I am doing any mistake here?
#conftest.py
@pytest.fixture()
def json_loader(request):
"""Loads the data from given JSON file"""
def _loader(filename):
import json
with open(filename, 'r') as f:
data = json.load(f)
return data
return _loader
def pytest_runtest_setup(item,json_loader): #hook fails to use json_loader
data = json_loader("some_file.json")
print(data)
#do something useful here with data`
I get the following error when I run it.
pluggy.manager.PluginValidationError: Plugin ‘C:\some_path\conftest.py’ for hook ‘pytest_runtest_setup’ hookimpl definition: pytest_runtest_setup(item, json_loader) Argument(s) {‘json_loader’} are declared in the hookimpl but can not be found in the hookspec
Even if I do not pass json_loader as an arg to pytest_runtest_setup(), I get an error saying “Fixture “json_loader” called directly. Fixtures are not meant to be called directly”.
Version:
platform win32 – Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Can pytest hooks use fixtures? - Stack Overflow
It seems the only current supported way to dynamically instantiate fixtures is via the request fixture, specifically the getfixturevalue ...
Read more >Writing hook functions — pytest documentation
Hooks may be called both from fixtures or from other hooks. In both cases, hooks are called through the hook object, available in...
Read more >pytest fixtures: explicit, modular, scalable
Once pytest finds them, it runs those fixtures, captures what they returned (if anything), and passes those objects into the test function as...
Read more >How to use fixtures — pytest documentation
Once pytest figures out a linear order for the fixtures, it will run each one up until it returns or yields, and then...
Read more >Fixtures reference — pytest documentation
The conftest.py file serves as a means of providing fixtures for an entire directory. Fixtures defined in a conftest.py can be used by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not sure, in my view hooks are more advanced than fixtures, so fixture-based solutions are generally easier to understand by most users.
I don’t think this would work, fixtures and hooks have different semantics; trying to make hooks access fixtures (without hacks I mean) might be hard to do and raise all kind of problems.
In many cases you can extract the code from the fixture to a simple function, and just call that function from the fixture. This way you don’t duplicate any code.
The short answer is “no”, and there’s no easy way to include that support as well because hooks don’t have scope by definition, while fixtures are directly tied to a scope. Hooks might be called at any point in the session, and might be called externally in any order also.
I’ve often found that one doesn’t need fixtures in hooks, just need to approach the problem differently. Often using an
autouse
fixture you can get by the need to execute code before test setup for example, by per your snippet.