gpt4 book ai didi

python - 为什么在调用方法时无法识别 Pytest Parametrize?

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:13 25 4
gpt4 key购买 nike

我有这段 Python 代码:

import pytest
class Apple:

@pytest.mark.parametrize("kind", ['fruit', 'veg', 'nuts'])
def mix_kind_and_colour(self, kind):
print(kind)
return self
def runner():
return Apple.mix_kind_and_colour()
if __name__== "__main__":
runner()

但是我得到这个错误:

TypeError: mix_kind_and_colour() missing 2 required positional arguments: 'self' and 'kind'

我该如何解决?

最佳答案

Please have look at pytest's docu, it's quite fine.

在您想要对测试进行分组之前,您不需要类(class)。

但你需要遵循一些naming conventions .

用于从终端运行 pytest 命令的 test_sample.py 的一个简单工作示例是:

import pytest

@pytest.mark.parametrize("kind", ['fruit', 'veg', 'nuts'])
def test_mix_kind_and_colour(kind):
print(kind)

如果你想使用一个类尝试运行 pytest for:

import pytest

class TestApple:

@pytest.mark.parametrize("kind", ['fruit', 'veg', 'nuts'])
def test_mix_kind_and_colour(self, kind):
print(kind)

但您还需要一些 assertions为了测试什么?!?

# content of test_assert1.py
def f():
return 3

def test_function():
assert f() == 4

关于python - 为什么在调用方法时无法识别 Pytest Parametrize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53867229/

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