Use this tag for questions about pytest fixtures (built-in or defined using the @pytest.fixture decorator).
Questions tagged [pytest-fixtures]
36 questions
0
votes
1 answer
Change global state before the fixture
I'm writing a set of tests for methods of the object whose initialization may be altered by environment variables. I started by putting object creation into a fixture and making a bunch of tests
@pytest.fixture()
def my_object():
# Initialise and…

warownia1
- 2,771
- 1
- 22
- 30
0
votes
1 answer
How does pytest create fixtures, or, how to get my yield?
I have a generator function
def foo():
resource = setup()
yield resource
tidy(resource)
I'm using this as a fixture
@pytest.fixture
def foofix():
yield from foo()
This works fine.
I want to test foo
def test_foo():
res_gen =…

joel
- 6,359
- 2
- 30
- 55
0
votes
0 answers
Pytest indirect parametrization using anyio
Pytest offers a way to parametrize fixture just before running test function (documentation)
But I am trying to write async tests using AnyIO
Below is my modified code from example
import pytest
@pytest.fixture(scope="session")
def anyio_backend()…

Denis Khamitov
- 11
- 3
0
votes
1 answer
function vs class vs module vs package vs session for fixture scopes in Pytest
I set 5 fixtures with function, class, module, package and session scopes to test1() as shown below:
import pytest
@pytest.fixture(scope='function')
def fixture_function():
print('function')
@pytest.fixture(scope='class')
def fixture_class():
…

Super Kai - Kazuya Ito
- 22,221
- 10
- 124
- 129
0
votes
1 answer
How can I parametrize a function in pytest with a dynamic number of arguments that are based on another fixture?
I'm looking forward to do something similar to the solution described here:
Can I parameterize a pytest fixture with other fixtures?
But in my use case I want to have a dynamic number of tests based on a query in a database. In order to do the query…

nck
- 1,673
- 16
- 40
0
votes
0 answers
Python Selenium Fixture test setup failed (fixture 'init_driver' not found)
While i'm trying to run my test to test whether the fixture work or not, I'm getting the following error
test setup failed
file C:\Users\user\Desktop\ssqatest\ssqatest\tests\test_dummy.py, line 6
def test_dummy(self):
E fixture…
0
votes
2 answers
pytest autouse=True not working correctly? simply returning ["a"]
Here's the code
import pytest
@pytest.fixture
def first_entry():
return "a"
@pytest.fixture(autouse=True)
def append_first(first_entry):
return [first_entry]
def test_string_only(first_entry):
assert first_entry == ["a"]
simply…

ordem
- 130
- 2
- 9
0
votes
0 answers
ModuleNotFoundError: No module named 'backend' while running pytest
I am trying to test Airflow DAGs, so I created testing environment, Docker container with volume and connection to PostrgeSQL container.
When container runs, docker-entrypoint.sh installs all the packages, creates AIRFLOW_HOME temporary folder,…

Aidos Kenessov
- 21
- 2
0
votes
2 answers
pytest: Fixture shortcut to object inside another fixture breaks when object is updated
I have code that is essentially like this where the fixture foo is a shortcut to the foo object inside fixture myclass. This is purely for convenience so I don't have to do myclass.foo, however when myclass.foo gets updated, the foo fixture is not,…

jjj
- 767
- 2
- 9
- 14
0
votes
0 answers
I'm testing my django project with a pytest. argument of type 'News' is not iterable
While testing content I got:
TypeError: argument of type 'News' is not iterable
There is two asserts in my test.
I'm testing detail view of news page for anonymous user:
checks if the news is in the response context of the page
I check ordering…

Vladimir Shevchenko
- 25
- 5
0
votes
0 answers
Pytest with multiple parameter sets but one used dynamically based on some decision/option
Within Pytest, I'd like to have two different parameter sets:
"unit_type_a" = [
'a-param1',
'a-param2',
'a-param3'
]
"unit_type_b" = [
'b-param1',
'b-param2',
'b-param3',
…

seahawk
- 1
- 4
0
votes
0 answers
Re-run the decorated test in a pytest fixture
I'm trying to write a fixture to achieve such function:
do something;
run the test case;
do something;
re-run the test case
I write my fixture like this:
def dosomething1(path):
pass
def dosomething2(path):
pass
@pytest.fixture()
def…

faked human mao
- 3
- 2
0
votes
1 answer
How to use fixture return value in test method under a class
I have a fixture in conftest.py:
@pytest.fixture(scope="session", autouse=True)
def setup( server ):
global ws
if server == "Test1":
ws = create_connection(base_class.server1)
print("Websocket Connection Established…

AK1
- 1
- 2
0
votes
1 answer
TypeError: parametrize() got multiple values for argument 'indirect'
I am trying to parametrize my pytest, by getting value from external function, my method is like that:
import pytest
from Utilities.MiscUtils import generate_random_email_for_registration
from Utilities.TextFileUtils import…
0
votes
1 answer
pytest fixture not found when used in parametrize
@pytest.fixture
def text():
return "My fixture text for testing.\n"
@pytest.mark.parametrize(
("some_boolean_param", "text"),
[
(True, text),
(False, text),
],
)
def test_my_function(some_boolean_param, text,…

Nat G
- 191
- 1
- 15