小编vas*_*r c的帖子

pytest 钩子可以使用夹具吗?

我知道夹具可以使用其他夹具,但是钩子可以使用夹具吗?我在网上搜索了很多,但没有得到任何帮助。如果我在这里做错了,有人可以指出吗?

#conftest.py

@pytest.fixture()
def json_loader(request):   
    """Loads the data from given JSON file"""
    def _loader(filename):
        import json
        with open(filename, 'r') as f:
            data = json.load(f)
        return data
    return _loader



def pytest_runtest_setup(item,json_loader): #hook fails to use json_loader
    data = json_loader("some_file.json") 
    print(data) 
    #do something useful here with data
Run Code Online (Sandbox Code Playgroud)

运行时出现以下错误。

pluggy.manager.PluginValidationError: 插件 'C:\some_path\conftest.py' for hook 'pytest_runtest_setup' hookimpl 定义:pytest_runtest_setup(item, json_loader) Argument(s) {'json_loader'} 在 hookimpl 中声明但找不到在钩子规范中

即使我没有将 json_loader 作为 arg 传递给 pytest_runtest_setup(),我也会收到一条错误消息,指出“Fixture“json_loader”被直接调用。Fixtures 并不意味着被直接调用”

python pytest

6
推荐指数
1
解决办法
2973
查看次数

标签 统计

pytest ×1

python ×1