Hey all,
I have two queries about the request fixture. First one could be a possible documentation change, second is either a bug or my ignorance.
-
I noticed while working with indirect parametrization that the docs assume that the user knows request is a fixture and that it is the only parameter name allowed. I only found this out by reading a stack overflow comment (under accepted answer). Perhaps in the docs here one could state that request is a fixture.
-
Can one cleanly use a fixture for both parametrized and unparametrized tests, if the fixture includes request for use with indirect parametrization? The issue I am having is that I have a fixture that is creating some dummy data, and I want to use this dummy data for simple tests and also parametrized tests. What I reaqlly want I guess is a way to cleanly have request to have a default value. Code example from the pytest docs:
import pytest
@pytest.fixture
def fixt(request):
return request.param * 3
@pytest.mark.parametrize("fixt", ["a", "b"], indirect=True)
def test_indirect(fixt):
assert len(fixt) == 3
def test_direct(fixt): # fails because request.param doesn't exist
assert len(fixt) == 3
Traceback:
test setup failed
request = <SubRequest 'fixt' for <Function test_direct>>
@pytest.fixture
def fixt(request):
> return request.param * 3
E AttributeError: 'SubRequest' object has no attribute 'param'
My current fix is to add this check to the fixture:
@pytest.fixture
def fixt(request):
if hasattr(request, "param"):
p = request.param
else:
p = "x" # this is my default value
return p * 3
pytest and operating system versions
pytest 8.3.4
OS: Microsoft Windows 10
Hey all,
I have two queries about the
requestfixture. First one could be a possible documentation change, second is either a bug or my ignorance.I noticed while working with indirect parametrization that the docs assume that the user knows
requestis a fixture and that it is the only parameter name allowed. I only found this out by reading a stack overflow comment (under accepted answer). Perhaps in the docs here one could state thatrequestis a fixture.Can one cleanly use a fixture for both parametrized and unparametrized tests, if the fixture includes
requestfor use with indirect parametrization? The issue I am having is that I have a fixture that is creating some dummy data, and I want to use this dummy data for simple tests and also parametrized tests. What I reaqlly want I guess is a way to cleanly haverequestto have a default value. Code example from the pytest docs:Traceback:
My current fix is to add this check to the fixture:
pytest and operating system versions
pytest 8.3.4
OS: Microsoft Windows 10