繁体   English   中英

pytest通过配置文件运行标记

[英]pytest run markers via configuration file

我正在使用pytest进行一些功能测试以进行命令行测试。 我将测试保存在yml文件中,该文件用于测试配置。 我想在yml文件中提供一些可由pytest使用的标记,而不是在测试文件中使用装饰器方法。

例如

---
test_name: xyz
test_type: smoke
command: hello --help
expected: hello world
assertions:
  - testResult.actual_command_returnCode == 0
  - len(testResult.expected_value_result) != 0
  - testResult.expected_value_result[0] == testResult.actual_command_output

有没有一种方法可以在yml文件中指定标记,而pytest可以使用相同的标记来了解它需要运行哪些特定测试?

py.test不读取yaml文件,您将必须编写自己的代码来执行此操作,并将标记添加到测试函数中。 您可以通过在conftest.py文件中放置一个挂钩函数来做到这conftest.py 这是一个将标记smoke放置在名为test_a的测试上的test_a

def pytest_collection_modifyitems(session, config, items):
    for i in items:
        if i.name == "test_a":
            i.add_marker("smoke")

py.test会在从您的目录收集测试列表之后以及实际运行任何测试之前自动调用一次此函数(如果在conftest.py存在)。 您可以根据需要修改列表items

(请注意,我的示例仅查看测试名称,因此它将与任何测试文件中的名为test_a的函数匹配;您可能需要以其他方式选择要应用标记的实际测试,例如,比较i.nodeid的值i.nodeid ,它是一个类似path/file.py::function_name的字符串。)

这只是一个例子,您应该用读取yaml文件的代码替换代码,并根据在该文件中找到的内容来决定将哪些标记添加到哪些测试中。

我在conftest.py文件中创建了一个钩子,如下所示

def pytest_collection_modifyitems(config,items):
  with open("C:\Users\xyz\mno\cli_automation\test_configuration\project\test_command.yml", 'r') as stream:
    data_loaded = yaml.load(stream)
    for i in items:
        if i.name == data_loaded.get("test_name"):
            i.add_marker(data_loaded.get("test_type"))
            pytest_runtest_call(i)

当我将pytest_runtest_call(i)作为AttributeError: 'Function' object has no attribute '_evalxfail'操作时,出现属性错误AttributeError: 'Function' object has no attribute '_evalxfail' 检入[ https://docs.pytest.org/en/2.8.7/_modules/_pytest/skipping.html]的源代码,它在evalxfail = item._evalxfail第102行失败

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM