admin管理员组文章数量:1122832
This is very hard for me to find in pytest documentation. That's why I'm asking here.
I have a fixture that is loading data.
import pytest
@pytest.fixture(scope="function")
def param_data(request):
with open(f'tests/fixtures/{request.param}.json') as f:
return json.load(f)
And with that, I want to test the execution of a function of 3 JSON files:
- tests/fixtures/data1.json
- tests/fixtures/data2.json
- tests/fixtures/data3.json
How can I do it using @pytest.mark.parametrize
? I mean...
@pytest.mark.parametrize(???)
def test_my_function(dict, expected):
# Test my_function() with the dict loaded by fixture and the expected result.
assert my_function(dict) == expected
I see examples of both usages, but not with both of them.
And, all the fixtures I see are fixed with a value return, not using request.param
.
This is very hard for me to find in pytest documentation. That's why I'm asking here.
I have a fixture that is loading data.
import pytest
@pytest.fixture(scope="function")
def param_data(request):
with open(f'tests/fixtures/{request.param}.json') as f:
return json.load(f)
And with that, I want to test the execution of a function of 3 JSON files:
- tests/fixtures/data1.json
- tests/fixtures/data2.json
- tests/fixtures/data3.json
How can I do it using @pytest.mark.parametrize
? I mean...
@pytest.mark.parametrize(???)
def test_my_function(dict, expected):
# Test my_function() with the dict loaded by fixture and the expected result.
assert my_function(dict) == expected
I see examples of both usages, but not with both of them.
And, all the fixtures I see are fixed with a value return, not using request.param
.
- I can return a tuple in my fixture, but I wonder if there is a beautiful way to do it. – jcfaracco Commented Nov 21, 2024 at 15:09
1 Answer
Reset to default 1Use indirect parametrization along with "normal" parameters.
import pytest
@pytest.fixture
def add_one(request):
return 1 + request.param
@pytest.mark.parametrize("add_one,expected", ((2, 3), (4, 5)), indirect=["add_one"])
def test_my_func(add_one, expected):
assert add_one == expected
本文标签: pythonHow to mix pytest fixtures that uses request and regular values for parametrizeStack Overflow
版权声明:本文标题:python - How to mix pytest fixtures that uses request and regular values for parametrize? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309755a1934132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论