英文:
Pytest overriding default value of a parameter for a fixture
问题
I have the following simple code:
import pytest
@pytest.fixture
def func_multiply(a, b, c=0):
return a * b * c
@pytest.mark.parametrize(
"a, b, c",
[
(5, 1, 5),
]
)
def test_bug(func_multiply):
assert func_multiply == 25
I expect the test to pass, but for some reason, the value for c
isn't getting passed into the fixture. The idea is that if another function uses the same fixture without passing in a value for c
, the default value of 0 would be used. In this case, though, where the value of c
is specified, the default should be overridden.
英文:
I have the following simple code:
import pytest
@pytest.fixture
def func_multiply(a,b,c=0):
return a*b*c
@pytest.mark.parametrize(
"a,b,c",
[
(5,1,5),
]
)
def test_bug(func_multiply):
assert func_multiply == 25
I expect the test to pass but for some reason the value for c isn't getting passed into the fixture. The idea is that if another function used the same fixture without passing in a value for c, the default value of 0 would be used. In this case though, where value of c is specified, the default should be overridden.
答案1
得分: 1
以下是翻译好的部分:
似乎问题出在 test_bug 函数的实现上。具体来说,看起来你没有实际调用 @pytest.mark.parametrize 装饰器中的参数来调用 func_multiply fixture。
为了解决这个问题,你可以修改 test_bug 函数,包括 a、b 和 c 参数,并将它们传递给 func_multiply,像这样:
def test_bug(func_multiply, a, b, c):
assert func_multiply(a, b, c) == 25
通过这样做,你将能够测试使用 @pytest.mark.parametrize 指定的参数的 fixture 的行为。
此外,请确保将 a、b 和 c 参数添加到 @pytest.mark.parametrize 装饰器中,像这样:
@pytest.mark.parametrize(
"a,b,c",
[
(5,1,5),
]
)
这将确保 a、b 和 c 的值正确传递到 test_bug 函数中。
以下是最终的代码:
import pytest
@pytest.fixture
def func_multiply():
def _func_multiply(a, b, c=0):
return a*b*c
return _func_multiply
@pytest.mark.parametrize(
"a,b,c",
[
(5,1,5),
]
)
def test_bug(func_multiply, a, b, c):
assert func_multiply(a, b, c) == 25
经过这些修改,测试现在应该通过,并且 fixture 应该按预期工作。
英文:
It seems that the problem lies in the implementation of the test_bug function. Specifically, it looks like you're not actually calling the func_multiply fixture with the parameters from the @pytest.mark.parametrize decorator.
To address this issue, you can modify the test_bug function to include the a, b, and c parameters and pass them to func_multiply like this:
def test_bug(func_multiply, a, b, c):
assert func_multiply(a, b, c) == 25
By doing this, you'll be able to test the behavior of the fixture with the parameters specified in @pytest.mark.parametrize.
Also, be sure to add the a, b, and c parameters to the @pytest.mark.parametrize decorator, like this:
@pytest.mark.parametrize(
"a,b,c",
[
(5,1,5),
]
)
This will ensure that the values for a, b, and c are correctly passed into the test_bug function.
Here's the final code:
import pytest
@pytest.fixture
def func_multiply():
def _func_multiply(a, b, c=0):
return a*b*c
return _func_multiply
@pytest.mark.parametrize(
"a,b,c",
[
(5,1,5),
]
)
def test_bug(func_multiply, a, b, c):
assert func_multiply(a, b, c) == 25
With these changes, the test should now pass and the fixture should behave as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论