vkt*_*vkt 0 python pytest python-3.7
我正在尝试使用 python 函数进行测试pytest,但出现此错误。
import pytest
class Example:
def __init__(self):
pass
@staticmethod
def test_func(list_arg):
return len(list_arg)
@pytest.fixture()
def example_instance():
return Example()
def test_util_fun():
empty_val = []
assert example_instance.test_func(empty_val) == 2
Run Code Online (Sandbox Code Playgroud)
错误 :
AttributeError: 'function' object has no attribute 'test_func'
Run Code Online (Sandbox Code Playgroud)
pytest 和 python 版本
python 3.7
pytest 4.5.0
Run Code Online (Sandbox Code Playgroud)
您需要example_instance作为参数传递给test_util_fun; 它不会仅仅因为它是一个固定装置而神奇地出现:
def test_util_fun(example_instance):
empty_val = []
assert example_instance.test_func(empty_val) == 2
Run Code Online (Sandbox Code Playgroud)
固定装置的规则pytest有点不同。如果省略它,则将example_instance绑定到函数对象本身。
但是,如果将其指定为参数,pytest则将在需要时根据指定的范围自动实例化夹具,并将其传递给测试函数。