Dom Dom - 1 year ago 189
Python Question

How to rename test runs in Testrails

I'm trying to automatically rename test runs from the "Automated Run TIMESTAMP" that is generated by default.

What I would like in an ideal situation is for the pytest runner to grab part of the name from a json file within the test directory, and combine that with the relative path of the test I am running. The tests are within a virtual env if that makes any difference.

relative path:


content of test.json (which is in workspace/):

{ "testrail" : "Project Name" }

command prompt execution (from the workspace/ dir):

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

expected name in Testrails is 'Project Name - Functional - Auth TIMESTAMP'

Dom Dom
Answer Source

OK so I was able to figure out a way to automatically rename the tests without needing to import from an external file.

change this in workspace/venv/test/lib/python2.7/site-packages/pytest_testrail/ - (path will vary)

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

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:
    # Remove the Testrails location
    if '..' in locations:
    # Check length of list to determine if running individual test
    if len(locations) == 1:
        location = locations[0]

    tr_keys = get_testrail_keys(items)

This results in a testrun from workspace/functional/auth/ to be named as 'Functional - Auth Tests TIMESTAMP' and tests from workspace/functional to be named as 'Functional - Project Tests TIMESTAMP'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download