Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a conftest.py file into several smaller conftest-like parts

Tags:

python

pytest

I've got a large conftest.py file that I wish to split into smaller parts, for two reasons:

  1. The file is very large (~1000 lines, including documentation)
  2. Some of the fixtures depend on other fixtures, and I have no reason to expose those other fixtures as part of the conftest "API" when users look for relevant fixtures

I am not aware of any mechanism provided by pytest to resolve conftest files in multiple locations within the same folder, so I contrived one, below:

import sys
import os


sys.path.append(os.path.dirname(__file__))


from _conftest_private_part_1 import *
from _conftest_private_part_2 import *
from _conftest_private_part_3 import *


@pytest.fixture
def a_fixture_that_is_part_of_the_public_conftest_api():
    pass

This works for my needs, but I do wonder if there is a better way.

like image 677
alkalinity Avatar asked Nov 21 '14 14:11

alkalinity


People also ask

Can we have multiple Conftest files?

You could also create more conftest files. A straightforward solution is to separate tests for example into unit/integrations/ui and keep conftest files there.

What is Conftest file in Python?

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 any test in that package without needing to import them (pytest will automatically discover them).

What does Conftest mean?

Conftest is a utility to help you write tests against structured configuration data. For instance, you could write tests for your Kubernetes configurations, Tekton pipeline definitions, Terraform code, Serverless configs or any other structured data.

Where do I put Conftest py files?

You can put fixtures into individual test files, but to share fixtures among multiple test files, you need to use a conftest.py file somewhere centrally located for all of the tests. For the Tasks project, all of the fixtures will be in tasks_proj/tests/conftest.py. From there, the fixtures can be shared by any test.

Can I have more than one conftest py file?

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

What is conftest in pytest?

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 any test in that package without needing to import them (pytest will automatically discover them).

How to split a file into several lines in Python?

The output of the above program is shown in the snapshot below: The data in the file is split into several lines and each line is returned as an element in the list by making use of a split function called the splitlines () function in Python.

How to use conftest to test fixtures in Python?

Create a new file conftest.py and add the below code into it − Now, we have the files test_div_by_3_6.py and test_div_by_13.py making use of the fixture defined in conftest.py. The tests will look for fixture in the same file. As the fixture is not found in the file, it will check for fixture in conftest.py file.


2 Answers

You can put your stuff in other modules and reference them using a pytest_plugins variable in your conftest.py:

pytest_plugins = ['module1', 'module2'] 

This will also work if your conftest.py has hooks on them.

like image 185
Bruno Oliveira Avatar answered Oct 03 '22 00:10

Bruno Oliveira


You shouldn't need any fancy magic for that. py.test automatically adds the path of the current test file to sys.path, as well as all parent paths up to the directory it was targeted at.

Because of that, you don't even need to put that shared code into a conftest.py. You can just put into plain modules or packages and then import it (if you want to share fixtures, those have to be in a conftest.py).

Also, there is this note about importing from conftest.py in the documentation:

If you have conftest.py files which do not reside in a python package directory (i.e. one containing an __init__.py) then “import conftest” can be ambiguous because there might be other conftest.py files as well on your PYTHONPATH or sys.path. It is thus good practise for projects to either put conftest.py under a package scope or to never import anything from a conftest.py file.

like image 42
Björn Pollex Avatar answered Oct 02 '22 23:10

Björn Pollex