如何在 Testrail 中自动重命名测试运行

How to rename test runs in Testrail automatically

我正在尝试从默认生成的 "Automated Run TIMESTAMP" 自动重命名测试运行。

在理想情况下,我希望 pytest 运行器从测试目录中的 json 文件中获取部分名称,并将其与测试的相对路径结合起来 运行。如果有任何区别,测试将在虚拟环境中进行。

相对路径:

workspace/functional/auth/Test.py

test.json 的内容(在工作空间/):

{ "testrail" : "Project Name" }

命令提示符执行(从工作区/目录):

$ py.test --testrail=testrail.cfg functional/auth/Test.py

Testrails 中的预期名称是 'Project Name - Functional - Auth TIMESTAMP'

好的,所以我找到了一种无需从外部文件导入即可自动重命名测试的方法。

在 workspace/venv/test/lib/python2.7/site-packages/pytest_testrail/plugin.py 中更改 -(路径会有所不同)

import os

def testrun_name(location):
"""
Returns testrun name with timestamp
Edited to grab the directory and name the test based off of that
"""
projects = {mapping of projects}
suite = {mapping of sub folders to function names}

absolute_path = os.getcwd()
project_dir = absolute_path.split('workspace/')[1]

now = datetime.utcnow()
return '{} - {} Tests {}'.format(projects[project_dir],suite[location],now.strftime(DT_FORMAT))

@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(self, session, config, items):

    # Create list of locations of the test cases
    locations = []
    location = 'project'
    for item in items:
        loc = item.location[0].split('/')[1]
        if loc not in locations:
            locations.append(loc)
    # Remove the Testrails location
    if '..' in locations:
        locations.remove('..')
    # Check length of list to determine if running individual test
    if len(locations) == 1:
        location = locations[0]

    tr_keys = get_testrail_keys(items)
    self.create_test_run(
        self.assign_user_id,
        self.project_id,
        self.suite_id,
        testrun_name(location),
        tr_keys
    )

这导致来自 workspace/functional/auth/Test.py 的测试运行被命名为 'Functional - Auth Tests TIMESTAMP' 并且来自 workspace/functional 的测试被命名为 'Functional - Project Tests TIMESTAMP'