将元数据添加到 pytest_configure 文件中,出现内部错误 AttributeError:“Config”对象没有属性“metadata”

问题描述 投票:0回答:3

我已经导入了 pytest 并安装了 pytest html,我正在尝试将元数据添加到 pytest_configure 文件中,但是当我在终端中运行测试时,我得到内部服务器错误属性未找到

这是conftest文件中的内容:

#hook for adding environment info to html report
def pytest_configure(config):
    config.metadata['Project Name'] = 'Hybrid Framework Practice' 
    config.metadata['Module Name'] = 'Customers'
    config.metadata['Tester'] = 'Amar'

#hook for delete/modify environment info to html report

@pytest.mark.optionalhook
def pytest_metadata(metadata):
    metadata.pop("JAVA_HOME", None)
    metadata.pop("Plugins", None)

这是我在终端中运行的:

pytest -v -s -n=2 --html=Reports\report.html testCases\test_login.py --browser chrome

我在 youtube 上关注一个人,它似乎使用完全相同的代码为他运行,但对我来说它失败了。 在没有 pytest_configure 代码的情况下运行上面的行可以正常工作并且测试通过 有谁知道缺少什么? 谢谢

python selenium frameworks pytest pytest-html
3个回答
2
投票

安装 pytest-html 和 pytest-metadata 后,我仍然遇到 AttributeError。根据这篇文章,问题似乎出在语法上。根据您的示例,以下版本对我有用:

 config._metadata = {
            "Tester": "Amar",
            "Project Name": "Hybrid Framework Practice",
        }

在 pytest 中更新“config._metadata”字典的正确方法是为其分配一个新字典。原因是pytest中的config._metadata属性不是常规字典,而是特殊的元数据对象。当您为其分配新字典时,您将用包含自定义元数据的新对象替换现有元数据对象。

另一方面,只有当 config._metadata 对象已经初始化为字典时,使用 config._metadata['Project Name'] = 'Hybrid Framework Practice' 才有效。如果未初始化或类型不同,则使用方括号分配新的键值对会引发错误。

我希望这有帮助!


0
投票

嘿,只需用 _metadata 替换元数据即可。

例如:

config._metadata['Project Name'] = 'Hybrid Framework Practice' 

0
投票

将代码片段替换为以下内容。它可能会起作用:

def pytest_configure(config):
config._metadata = {
    "Project Name": "Hybrid Framework Practice",
    "Module Name": "Customers",
    "Tester":"Amar"
}
© www.soinside.com 2019 - 2024. All rights reserved.