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

时间:2016-11-21 22:35:02

标签: python command-line pytest testrail

我试图自动重命名测试运行来自"自动运行TIMESTAMP"这是默认生成的。

在理想情况下我想要的是pytest运行器从测试目录中的json文件中获取部分名称,并将其与我正在运行的测试的相对路径相结合。如果有任何不同,测试都在虚拟环境中。

相对路径:

workspace/functional/auth/Test.py

test.json的内容(在工作区/中):

{ "testrail" : "Project Name" }

命令提示符执行(来自工作空间/目录):

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

Testrails中的预期名称是'项目名称 - 功能 - 认证TIMESTAMP'

1 个答案:

答案 0 :(得分:1)

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

在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的testrun命名为'功能 - 验证测试TIMESTAMP'并将工作空间/功能测试命名为'功能 - 项目测试TIMESTAMP'