Questions tagged [conftest]
56 questions
538
votes
6 answers
What is conftest.py for in Pytest?
I'm trying to understand what conftest.py files are meant to be used for.
In my (currently small) test suite I have one conftest.py file at the project root. I use it to define the fixtures that I inject into my tests.
I have two questions:
Is this…

Aviv Cohn
- 15,543
- 25
- 68
- 131
6
votes
1 answer
Is it discouraged to import conftest.py within test-modules?
I am creating an object within conftest.py and use it in some fixtures. I also need to use this object within my test-modules. Currently I am importing conftest.py inside my test-modules and make use of that 'helper' object. I am pretty sure that…

PedarePessareShoja
- 61
- 1
- 4
5
votes
1 answer
python vscode typehints for conftest fixtures
For my tests I defined a lot of fixtures and also added typehints to them. All of this fixtures are defined in the conftest.py and are "magically" available in all of the test :)
But sadly vscode can't show information (signatures, typehints, etc)…

jacksbox
- 911
- 1
- 11
- 24
4
votes
3 answers
Why is pytest giving me an "unrecognized option" error when I try and create a custom command line option?
I'm using Python 3.8 and pytest 6.0.1. How do I create a custom command line option for pytest? I thought it was as simple as adding this to conftest.py…

Dave
- 15,639
- 133
- 442
- 830
4
votes
1 answer
How do I activate a PyTest fixture from the command line?
I'm using Python3.8 and pytest. How can I activate a fixture for my session based on some command line specification? Right now I have a tests/conftest.py file that includes
@pytest.fixture(name="fixture1", scope="session")
def fixture1():
…

Dave
- 15,639
- 133
- 442
- 830
3
votes
0 answers
How to distribute conftest.py and fixtures from a project across different repo/projects?
I could not find a solid example on doing so. Here is the situation.
I have several fixtures written in a Python file called my_fixtures.py & a conftest.py containing configuration for the fixtures like below:
import pytest
from glob import glob …

Unknown
- 778
- 1
- 7
- 16
3
votes
0 answers
In PyTest, how can I specify a different conftest.py file to use?
I'm using Python3.8 with PyTest. By default, pytest uses
./conftest.py
to build and import fixtures. Is it possible to launch pytest and use a different file to load the fixtures -- i.e. specify a different file other than conftest.py?

Dave
- 15,639
- 133
- 442
- 830
3
votes
0 answers
Is there a way to add custom CLI arguments to pytest using the same syntax as would be used with the argparse module?
TLDR:
I want to extent pytest's cli and I know how that can be done using their own API, but I was just wondering if there was a way to do so using the argparse module directly (or its syntax at least) since I already have all the cli arguments I…

Anonymous007
- 31
- 3
3
votes
4 answers
How to apply fixture from conftest.py to inner folders only
I have a fixture that located in conftest.py.
@pytest.fixture(scope='module', autouse=True)
def my_fixture():
"""
Some useful code
"""
The structure is like below:
tests
|
|--first_folder
| |--__init__.py
| |--test_first_1.py
…

JaSON
- 4,843
- 2
- 8
- 15
2
votes
0 answers
map pytest to specific conftest location
I couldn't find anything in the documentation and so I thought i'd try here..
I'm trying to make a pytest run a file located separately from the folder where the conftest is located, but to use the conftest.
for example - running a test under…

liormayn
- 203
- 3
- 12
1
vote
2 answers
How can I run the same test for every module without duplicating code?
I have a project with multiple modules that all generally require the same type of test. Each one imports a file with the same name, and given that file, all of them have the same test.
# test/integration/app1_test.py
from app1 import app
def…

Harsha Nandiwada
- 19
- 4
1
vote
1 answer
Using own decorators with pytest
I would like to use a "helper" decorator in multiple pytest test files:
min311 = pytest.mark.skipif(sys.version_info < (3,11), reason="You need at least Python v3.11 to run this test")
...
@min311
def test_...
Which is the best place for min311?…

FERcsI
- 388
- 1
- 10
1
vote
1 answer
Empty __init__ and conftest files
Is there any usage of empty __init__.py or conftest.py files in Python? Today I've had a discussion about it in my team and someone said that they may have an importance in testing with Pytest and checking code coverage.

Maciej Dałek
- 39
- 4
1
vote
1 answer
Conftest Policy for Kubernetes manifests for checking that images come from a specific registry
I'm using conftest for validating policies on Kubernetes manifests.
Below policy validates that images in StatefulSet manifests have to come from specific registry reg_url
package main
deny[msg] {
input.kind == "StatefulSet"
not…

rok
- 9,403
- 17
- 70
- 126
1
vote
1 answer
How to read the docstring of test functions from a fixture?
I was trying to get the docstrings of all the test functions from a fixture defined in conftest.py, as shown in the code below, so that they can be analyzed for purposes.
But, from here how can I access the __doc__ attribute of that function when…

gthy
- 301
- 1
- 3
- 10