In pytest, what is the use of files?


I’m trying to understand what files are meant to be used for.

In my (currently small) test suite I have one file at the project root. I use it to define the fixtures that I inject into my tests.

I have two questions:

  1. Is this the correct use of Does it have other uses?
  2. Can I have more than one file? When would I want to do that? Examples will be appreciated.

More generally, how would you define the purpose and correct use of file(s) in a pytest test suite?

Asked By: Aviv Cohn



In a wide meaning is a local per-directory plugin. Here you define directory-specific hooks and fixtures. In my case a have a root directory containing project specific tests directories. Some common magic is stationed in ‘root’ Project specific – in their own ones. Can’t see anything bad in storing fixtures in unless they are not used widely (In that case I prefer to define them in test files directly)

Answered By: Furious Duck

Is this the correct use of

Yes it is. Fixtures are a potential and common use of The
fixtures that you will define will be shared among all tests in your test suite. However, defining fixtures in the root might be useless and it would slow down testing if such fixtures are not used by all tests.

Does it have other uses?

Yes it does.

  • Fixtures: Define fixtures for static data used by tests. This data can be accessed by all tests in the suite unless specified otherwise. This could be data as well as helpers of modules which will be passed to all tests.

  • External plugin loading: is used to import external plugins or modules. By defining the following global variable, pytest will load the module and make it available for its test. Plugins are generally files defined in your project or other modules which might be needed in your tests. You can also load a set of predefined plugins as explained here.

    pytest_plugins = "someapp.someplugin"

  • Hooks: You can specify hooks such as setup and teardown methods and much more to improve your tests. For a set of available hooks, read Hooks link. Example:

      def pytest_runtest_setup(item):
           """ called before ``pytest_runtest_call(item). """
           #do some stuff`
  • Test root path: This is a bit of a hidden feature. By defining in your root path, you will have pytest recognizing your application modules without specifying PYTHONPATH. In the background, py.test modifies your sys.path by including all submodules which are found from the root path.

Can I have more than one file?

Yes you can and it is strongly recommended if your test structure is somewhat complex. files have directory scope. Therefore, creating targeted fixtures and helpers is good practice.

When would I want to do that? Examples will be appreciated.

Several cases could fit:

Creating a set of tools or hooks for a particular group of tests.


def pytest_runtest_setup(item):
    print("I am mod")
    #do some stuff

test root/mod2/ will NOT produce "I am mod"

Loading a set of fixtures for some tests but not for others.


def fixture():
    return "some stuff"


def fixture():
    return "some other stuff"


def test(fixture):

Will print "some other stuff".

Overriding hooks inherited from the root


def pytest_runtest_setup(item):
    print("I am mod")
    #do some stuff


def pytest_runtest_setup(item):
    print("I am root")
    #do some stuff

By running any test inside root/mod, only "I am mod" is printed.

You can read more about here.


What if I need plain-old helper functions to be called from a number
of tests in different modules – will they be available to me if I put
them in a Or should I simply put them in a
module and import and use it in my test modules?

You can use to define your helpers. However, you should follow common practice. Helpers can be used as fixtures at least in pytest. For example in my tests I have a mock redis helper which I inject into my tests this way.


def mock_redis():
    return MockRedis()




def test(mock_redis):

This will be a test module that you can freely import in your tests. NOTE that you could potentially name as if your module redis contains more tests. However, that practice is discouraged because of ambiguity.

If you want to use, you can simply put that helper in your root and inject it when needed.


def mock_redis():
    return MockRedis()


def test(mock_redis):

Another thing you can do is to write an installable plugin. In that case your helper can be written anywhere but it needs to define an entry point to be installed in your and other potential test frameworks. See this.

If you don’t want to use fixtures, you could of course define a simple helper and just use the plain old import wherever it is needed.


class MockRedis():
    # stuff


from helper.redis import MockRedis

def test():

However, here you might have problems with the path since the module is not in a child folder of the test. You should be able to overcome this (not tested) by adding an to your helper


from .redis import MockRedis

Or simply adding the helper module to your PYTHONPATH.

Answered By: Simone Zandara

I use the file to define the fixtures that I inject into my tests, is this the correct use of

Yes, a fixture is usually used to get data ready for multiple tests.

Does it have other uses?

Yes, a fixture is a function that is run by pytest before, and sometimes
after, the actual test functions. The code in the fixture can do whatever you
want it to. For instance, a fixture can be used to get a data set for the tests to work on, or a fixture can also be used to get a system into a known state before running a test.

Can I have more than one file? When would I want to do that?

First, it is possible to put fixtures into individual test files. However, to share fixtures among multiple test files, you need to use a file somewhere centrally located for all of the tests. Fixtures can be shared by any test. They can be put in individual test files if you want the fixture to only be used by tests in that file.

Second, yes, you can have other files in subdirectories of the top tests directory. If you do, fixtures defined in these lower-level files will be available to tests in that directory and subdirectories.

Finally, putting fixtures in the file at the test root will make them available in all test files.

Answered By: lmiguelvargasf

Here are the official docs about using to share fixtures: sharing fixtures across multiple files

The file serves as a means of providing fixtures for an entire directory. Fixtures defined in a can be used by any test in that package without needing to import them (pytest will automatically discover them).

You can have multiple nested directories/packages containing your tests, and each directory can have its own with its own fixtures, adding on to the ones provided by the files in parent directories.

Answered By: Martin Thøgersen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.