Dom Dom - 5 days ago 5
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:

workspace/functional/auth/Test.py


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/Test.py


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

Dom Dom
Answer

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/plugin.py - (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))

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

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

Comments