I have a fixture which in I want to use request.param
multiple times.
@pytest.fixture
def get_settings(request):
with mock.patch("helpers.helpers.get_settings") as mocked_settings:
mocked_settings.return_value = Settings(
TOKEN_KEY=request.param, # <== Here
TOKEN_PASSWORD=request.param) # <== And here
yield
@pytest.fixture
def get_user():
return User(name="Test User")
def generate_new_token(user: User) -> str:
settings = get_settings()
private_key = settings.TOKEN_KEY
token_password = settings.TOKEN_PASSWORD
if not private_key.strip():
raise ValueError("Private key must not be empty")
How can I do it? If I do something like above (with two request.param
), and then use it in my test function like this:
@pytest.mark.parametrize('get_settings, expected', [(FAKE_TOKEN_KEY, '', None), (FAKE_TOKEN_KEY, '1', None)],
indirect=["get_settings"])
def test_generate_new_token(get_user, get_settings, expected):
assert generate_new_token(user=get_user) == expected
I get this error:
in "parametrize" the number of names (2): ['get_settings', 'expected'] must be equal to the number of values (3)
I'm not sure if it's related to the second fixture (get_user
) I'm passing to the test function or not. But how can I fix it?
UPDATE
None
is the expected value and I put the other two in tuple. But in this case I get following error:
test setup failed
request = <SubRequest 'get_settings' for <Function test_generate_new_token[get_settings1-None]>> @pytest.fixture def get_settings(request): with mock.patch("helpers.helpers.get_settings") as mocked_settings: > mocked_settings.return_value = Settings( DEBUG=True, TOKEN_KEY=request.param, # FAKE_TOKEN_KEY, TOKEN_PASSWORD=request.param)
../conftest.py:76:
pydantic/env_settings.py:36: in pydantic.env_settings.BaseSettings.init ???
???
E pydantic.error_wrappers.ValidationError: 2 validation errors for Settings E TOKEN_KEY
E str type expected (type=type_error.str)
E TOKEN_PASSWORD
E str type expected (type=type_error.str)
pydantic/main.py:406: ValidationError