question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can pytest hooks use fixtures?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoddemuscommented, Mar 30, 2019

I had actually used ‘autouse’ fixture before to get this work done before every test run. But then, I realized I should be using ‘pytest_runtest_setup’ hook to make it look more neat and to justify the purpose of this hook. Also any new team member would easily understand if I had used this hook rather than trying to accomplish the same using ‘autouse’ fixture, as he would obviously know what this hook is used for.

Not sure, in my view hooks are more advanced than fixtures, so fixture-based solutions are generally easier to understand by most users.

If yes, should we raise a bug asking for a feature where hooks can use fixtures?

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.

Just for eg:, I can still use ‘pytest_runtest_setup’ in my design but then I would have to re-write the code for json loading. That means we would be duplicating that code when simply I could have utilized ‘json_loader’ fixture.

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.

1reaction
nicoddemuscommented, Mar 29, 2019

I 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?

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found