简体   繁体   English

pytest通过配置文件运行标记

[英]pytest run markers via configuration file

I am running some functional tests using pytest for command line testing. 我正在使用pytest进行一些功能测试以进行命令行测试。 I have my tests in a yml file which is used for test configuration. 我将测试保存在yml文件中,该文件用于测试配置。 I want to provide some markers in my yml file which can be used by pytest instead of using the decorator approach inside the test file. 我想在yml文件中提供一些可由pytest使用的标记,而不是在测试文件中使用装饰器方法。

eg 例如

---
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

Is there a way i can specify markers in my yml file and pytest can used the same for understanding which specific tests it needs to run? 有没有一种方法可以在yml文件中指定标记,而pytest可以使用相同的标记来了解它需要运行哪些特定测试?

py.test does not read yaml files, you will have to write your own code that does that and adds markers to your test functions. py.test不读取yaml文件,您将必须编写自己的代码来执行此操作,并将标记添加到测试函数中。 You can do that with a hook function placed in your conftest.py file. 您可以通过在conftest.py文件中放置一个挂钩函数来做到这conftest.py Here is an example that places a marker smoke on a test named test_a : 这是一个将标记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")

This function (if present in conftest.py ) is automatically called by py.test once, right after it collects a list of tests from your directory and before any tests are actually run. py.test会在从您的目录收集测试列表之后以及实际运行任何测试之前自动调用一次此函数(如果在conftest.py存在)。 You can modify the list items as you wish. 您可以根据需要修改列表items

(NOTE my example looks only at the test name, so it will match a function named test_a in any test file; you may need a different way of selecting the actual test to which to apply the marker, eg, compare the value of i.nodeid , which is a string like path/file.py::function_name .) (请注意,我的示例仅查看测试名称,因此它将与任何测试文件中的名为test_a的函数匹配;您可能需要以其他方式选择要应用标记的实际测试,例如,比较i.nodeid的值i.nodeid ,它是一个类似path/file.py::function_name的字符串。)

This is only an example, you should replace the code with something that reads your yaml file and decides which markers to add to what tests, depending on what it finds in that file. 这只是一个例子,您应该用读取yaml文件的代码替换代码,并根据在该文件中找到的内容来决定将哪些标记添加到哪些测试中。

I created a hook in my conftest.py file as follows 我在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)

I am getting an attribute error when i do the pytest_runtest_call(i) as AttributeError: 'Function' object has no attribute '_evalxfail' . 当我将pytest_runtest_call(i)作为AttributeError: 'Function' object has no attribute '_evalxfail'操作时,出现属性错误AttributeError: 'Function' object has no attribute '_evalxfail' Checked in the source code of [ https://docs.pytest.org/en/2.8.7/_modules/_pytest/skipping.html] , its failing at line 102 at evalxfail = item._evalxfail not sure why 检入[ 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