How to use py.test fixtures without importing them

10,503

Fixtures and their visibility are a bit odd in pytest. They don't require importing, but if you defined them in a test_*.py file, they'll only be available in that file.

You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files.

pytest-internal fixtures are simply defined in a core plugin, and thus available everywhere. In fact, a conftest.py is basically nothing else than a per-directory plugin.

You can also run py.test --fixtures to see where fixtures are coming from.

Share:
10,503

Related videos on Youtube

Dave Halter
Author by

Dave Halter

Author of the Python autocompletion framework Jedi and its VIM-plugin jedi-vim. I love Python, VIM, PostgreSQL, Linux and Bash. If you are interested in my work, follow me on github.

Updated on June 04, 2022

Comments

  • Dave Halter
    Dave Halter about 2 years

    Say I have a file fixtures.py that defines a simple py.test fixture called foobar.

    Normally I would have to import that fixture to use it (including all of the sub-fixtures), like this:

    from fixtures import foobar
    
    def test_bazinga(foobar):
    

    Note that I also don't want to use a star import.

    How do I import this fixture so that I can just write:

    import fixtures
    
    def test_bazinga(foobar):
    

    Is this even possible? It seems like it, because py.test itself defines exactly such fixtures (e.g. monkeypatch).

    • Pedru
      Pedru
      put your fixtures in conftest.py