Pytest覆盖夹具的参数默认值

huangapple go评论131阅读模式
英文:

Pytest overriding default value of a parameter for a fixture

问题

I have the following simple code:

  1. import pytest
  2. @pytest.fixture
  3. def func_multiply(a, b, c=0):
  4. return a * b * c
  5. @pytest.mark.parametrize(
  6. "a, b, c",
  7. [
  8. (5, 1, 5),
  9. ]
  10. )
  11. def test_bug(func_multiply):
  12. 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:

  1. import pytest
  2. @pytest.fixture
  3. def func_multiply(a,b,c=0):
  4. return a*b*c
  5. @pytest.mark.parametrize(
  6. "a,b,c",
  7. [
  8. (5,1,5),
  9. ]
  10. )
  11. def test_bug(func_multiply):
  12. 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 函数,包括 abc 参数,并将它们传递给 func_multiply,像这样:

  1. def test_bug(func_multiply, a, b, c):
  2. assert func_multiply(a, b, c) == 25

通过这样做,你将能够测试使用 @pytest.mark.parametrize 指定的参数的 fixture 的行为。

此外,请确保将 abc 参数添加到 @pytest.mark.parametrize 装饰器中,像这样:

  1. @pytest.mark.parametrize(
  2. "a,b,c",
  3. [
  4. (5,1,5),
  5. ]
  6. )

这将确保 abc 的值正确传递到 test_bug 函数中。

以下是最终的代码:

  1. import pytest
  2. @pytest.fixture
  3. def func_multiply():
  4. def _func_multiply(a, b, c=0):
  5. return a*b*c
  6. return _func_multiply
  7. @pytest.mark.parametrize(
  8. "a,b,c",
  9. [
  10. (5,1,5),
  11. ]
  12. )
  13. def test_bug(func_multiply, a, b, c):
  14. 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:

  1. def test_bug(func_multiply, a, b, c):
  2. 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:

  1. @pytest.mark.parametrize(
  2. "a,b,c",
  3. [
  4. (5,1,5),
  5. ]
  6. )

This will ensure that the values for a, b, and c are correctly passed into the test_bug function.

Here's the final code:

  1. import pytest
  2. @pytest.fixture
  3. def func_multiply():
  4. def _func_multiply(a, b, c=0):
  5. return a*b*c
  6. return _func_multiply
  7. @pytest.mark.parametrize(
  8. "a,b,c",
  9. [
  10. (5,1,5),
  11. ]
  12. )
  13. def test_bug(func_multiply, a, b, c):
  14. assert func_multiply(a, b, c) == 25

With these changes, the test should now pass and the fixture should behave as expected.

huangapple
  • 本文由 发表于 2023年4月20日 09:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定