gpt4 book ai didi

python - 类型错误 : missing 1 required positional argument while using pytest fixture

转载 作者:行者123 更新时间:2023-12-04 13:08:05 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





passing a py.test fixture between test files in a module

(1 个回答)


5 个月前关闭。




我已经在一个文件中编写了我的测试类,我正在尝试使用 pytest fixtures这样我就不必在每个测试函数中创建相同的输入数据。下面是最小的工作示例。

import unittest
import pytest

@pytest.fixture
def base_value():
return 5

class Test(unittest.TestCase):

def test_add_two(self, base_value):
result = base_value + 2
self.assertEqual(result, 7, "Result doesn't match")
但是,当我使用 pytest-3 对此进行测试时,出现以下错误:

TypeError: test_add_two() missing 1 required positional argument:'base_value'


这让我感到困惑,因为 base_value 明确作为 test_add_two 的参数之一给出。 .任何帮助都受到高度赞赏。

最佳答案

这是因为你在混pytestunittest .尝试

@pytest.fixture
def base_value():
return 5


class Test:
def test_add_two(self, base_value):
result = base_value + 2
assert result == 7, "Result doesn't match"
如果失败,错误将是
def test_add_two(self, base_value):
result = base_value + 2
> assert result == 8, "Result doesn't match"
E AssertionError: Result doesn't match
E assert 7 == 8
但是 pytest 与 unittest 不兼容吗?
仅在有限的基础上。来自 Pytest unittest.TestCase Support

pytest features in unittest.TestCase subclasses The following pytestfeatures work in unittest.TestCase subclasses:

  • Marks: skip, skipif, xfail;
  • Auto-use fixtures;

The following pytest features do not work, and probably never will dueto different design philosophies:

  • Fixtures (except for autouse fixtures, see below);
  • Parametrization;
  • Custom hooks;

Third party plugins may or may not work well, depending on the pluginand the test suite.

关于python - 类型错误 : missing 1 required positional argument while using pytest fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68439799/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com