简体   繁体   English

收集 pytest 中实施的所有测试以及标记

[英]Collecting all the tests implemented in pytest along with markers

I am looking forward to list all the (pytest) test cases in the form of CSV.我期待以 CSV 的形式列出所有 (pytest) 测试用例。

Use case scenario: We already have test cases defined for multiple platforms and databases.用例场景:我们已经为多个平台和数据库定义了测试用例。 These test cases are organized/grouped based on different markers (Ex: platform_linux, platform_centos, db_maria, db_mongo, db_redis etc).这些测试用例根据不同的标记进行组织/分组(例如:platform_linux、platform_centos、db_maria、db_mongo、db_redis 等)。

I want to create a CSV file, that contains all the markers and test cases that belong the marker.我想创建一个 CSV 文件,其中包含属于该标记的所有标记和测试用例。

Sample of CSV file: CSV 文件样本:

> Marker, Test Cases
> platform_linux, [test_linux1, test_linux2...]
> db_mariadb, [test_mariadb1, test_mariadb2...]

Goal here is to process the CSV file and derive all the tests running on Linux platform with mariadb.这里的目标是处理 CSV 文件,并派生出运行在 Linux 平台上的所有测试与 mariadb。

I did get get some answers on collecting test cases (--collect-only) can it be used to create desired CSV?, Do we have existing plugins to achieve the same?我确实得到了一些关于收集测试用例的答案(--collect-only)它可以用来创建所需的 CSV 吗?我们是否有现有的插件来实现相同的目的?

I'm not aware of a suitable plugin (though there are hundreds of them, so it may still exist), but you can implement this yourself by implementing pytest_collection_modifyitems .我不知道合适的插件(虽然有数百个,所以它可能仍然存在),但你可以通过实现pytest_collection_modifyitems自己实现它。 You get there in the collection phase after all items are collected.收集完所有物品后,您将进入收集阶段。 The collected test items are passed there as a list, which you may use to write out the csv.收集到的测试项目以列表的形式传递到那里,您可以使用它来写出 csv。

You would probably make this dependent on some option with the file name as value.您可能会使它依赖于以文件名作为值的某些选项。 Here is an example:这是一个例子:

top-level conftest.py顶级conftest.py

import csv

def pytest_addoption(parser):
    parser.addoption("--output-tests-csv", action="store", default=None)

def pytest_collection_modifyitems(config, items):
    csv_out = config.getoption("--output-tests-csv")
    if csv_out:
        tests = {}
        # collect all tests with matching markers
        for item in items:
            for mark in item.iter_markers():
                if mark.name.startswith(("platform_", "db_")):
                    tests.setdefault(mark.name, []).append(item.name)
        # write tests in csv (one line per marker)
        with open(csv_out, 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')
            for marker, tests in tests.items():
                writer.writerow([marker, tests])

This assumes that the markers to consider start with platform_ or db_ , you may have to adapt this, also the wanted csv format, but this should give you an idea.这假设要考虑的标记以platform_db_开始,您可能必须调整它,以及想要的 csv 格式,但这应该给您一个想法。

There is pytest-csv plugin that provides option to generate required CSV with markers, markers_as_parameters etc. pytest-csv 插件提供了生成所需 CSV 的选项,其中包含标记、markers_as_parameters 等。

https://github.com/nicoulaj/pytest-csv https://github.com/nicoulaj/pytest-csv

Sample usage (customizing the columns in the CSV):示例用法(自定义 CSV 中的列):

py.test --csv tests.csv --csv-columns host,function,status,duration,parameters_as_columns

Note: requirement pytest >= 6.0注:要求pytest >= 6.0

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

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