Use this tag for questions about pytest fixtures (built-in or defined using the @pytest.fixture decorator).
Questions tagged [pytest-fixtures]
36 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
37
votes
4 answers
When to use pytest fixtures?
I'm new to testing and I've stumbled across pytest fixtures, but I'm not entirely sure when to use them and why they're useful.
For example, see the below code:
import pytest
@pytest.fixture
def input_value():
input = 39
return input
def…

the man
- 1,131
- 1
- 8
- 19
2
votes
2 answers
How can I use the transformed data from the fixture for parameterization within the current test?
I have next code for pytest:
import pytest
@pytest.fixture
def my_fixture():
def _method(a_list):
return [num*0.5 for num in a_list]
return _method
# @pytest.mark.parametrize ??
def test_me(my_fixture):
pre_list = [0, 3, 1, 2]
…

KarimovAnton
- 25
- 3
2
votes
1 answer
Correct use of pytest fixtures of objects with Django
I am relatively new to pytest, so I understand the simple use of fixtures that looks like that:
@pytest.fixture
def example_data():
return "abc"
and then using it in a way like this:
def test_data(self, example_data):
assert example_data ==…

Flora Biletsiou
- 309
- 3
- 6
- 17
2
votes
2 answers
How to pass a list of fixtures to a test case in Pytest?
I have a case where I need a list of fixtures (similar ones) inside a test case and perform same operation with those fixtures.
For ex:
def testcase(fixture1, fixture2, fixture3):
# do some operation with fixture1
# do the same operation…

Underoos
- 4,708
- 8
- 42
- 85
2
votes
1 answer
Why are pytest fixtures not meant to be called directly from within tests?
I understand that pytest fixtures raise an error when calling a fixture directly from within a test. But I don't fully understand why. For context, I am a junior dev new to python so I may be missing something obvious that needs explaining.
I have a…

Tom
- 93
- 1
- 1
- 8
1
vote
1 answer
Cause of pytest ValueError: fixture is being applied more than once to the same function
I have a unit test I'm running with a particular fixture. The fixture is pretty simple and looks like so:
@pytest.fixture
def patch_s3():
# Create empty bucket
bucket = s3_resource().create_bucket(Bucket=BUCKET)
…

code11
- 1,986
- 5
- 29
- 37
1
vote
0 answers
Getting Fixture Not found while running or debuging from UI but No issue while running via terminal pycharm
when i run or debug a test in pytest, i got following error
def test_session_start_recording_post(self, user_token):
E fixture 'user_token' not found
available fixtures: _session_faker,…

Sudip Neupane
- 33
- 1
- 9
1
vote
1 answer
Teardown method from add_finalizer of PyTest fixture doesn't work
I'm trying to make a fixture for cross-browser tests with PyTest. From @pytest.mark.parametrize fixture gets "browser_name" and sets up correct webdriver in internal method. In the end of tests I tried to add a teardowm method to close browser, but…

Danila Sukhoparov
- 53
- 5
1
vote
0 answers
How to collect the error/exception from multiple pytest fixture as report all together?
I have defined 3 fixtures and set as autouse=True which gets executed for each test. I wanna print the summary for each fixture once all scoped fixture execution is done. Is there a way we can collect result and print summary. One solution I can…

Lad
- 11
- 1
1
vote
1 answer
pytest how to attach runtime info to test item for reporting
I've googled and read most of pytest documents but didn't find this topic.
I want to store runtime information(such as status code returned from server being tested) in some pytest item properites, to be used in reporting phase, particuarly add some…

Lei Yang
- 3,970
- 6
- 38
- 59
1
vote
0 answers
pytest fixtures naming as a variable
I have two pytest selenium fixtures:
wd - webdriver fixture (widely used for all webend tests)
wdwire - webdriverwire fixture (for request/response manipulating of webend)
Both work as they should, but I need to have a variable named like the…

MortenB
- 2,749
- 1
- 31
- 35
1
vote
1 answer
Is it possible for pytest to detect "-m 'not my_mark'" in a fixture?
It is possible to detect if a mark has been excluded?
I use pytest to run some tests against an embedded target. For some of the test setups, I can control the supply power via an epdu.
For the setups with an epdu, I want to power down the test…

nikolaj
- 180
- 1
- 10
1
vote
2 answers
how to rename a test name in pytest based on fixture param
Need to run same test on different devices. Used fixture to give ip addresses of the devices, and all tests run for the IPs provided by fixtures as requests. But at the same time, need to append the test name with the IP address to quickly analyze…

karthik
- 31
- 1
- 4
0
votes
1 answer
Pytest fixtures skipping test
I have this factory as ficture:
@pytest.fixture
def user_factory(db):
def create_app_user(
username: str,
password: str = None,
first_name: str = "firstname",
last_name: str = "lastname",
…

Muhammed Aboo_Anas
- 15
- 5