How to Integrate TestRail with Pytest?

Posted by: admin September 19, 2023 No Comments
Pytest Testrail

What is Pytest?

Pytest is one of the famous test automation framework which comes inbuilt with python. You can use this framework to automate API and Functional testing of desktop and web applications.

What is TestRail?

TestRail is a Test Management tool. In this tool you can create Test Plans, Test Runs, Test Cases and customize fields of test cases as per your project need. This tools is used at all levels of testing from Project Manager to Software Testing Engineer.

Why Integration is Needed

When we run automated tests of Smoke or Regression test cases, we have to update the results of each test case in the Test Management tool. In Test Management tools we can generate metrics based on results of each test case result and present to Test Leaders for analysis.

TestRail provides API for integration, you can refer documentation on this link Click Here

Let’s assume we have 100 Test Cases which are required to run under Smoke/Regression Test. The below steps need to be performed including Authentication, Creation of Test Run (New Test Case ID created for test cases included in Test Run), Update test cases inside test run based on results by automation scripts.

Sample Code for Authentication

client = APIClient('URL')
    client.user = 'UserID'
    client.password = 'Password'

Sample Code to Create Test Run and Assign new Test Case ID

 def createTestRun(self, test_ids=[], name="Test Run Name"):
        response = client.send_post(
            'add_run/1',
            {'suite_id': 1,
             'name': name,
             'include_all': False,
             'case_ids': test_ids
             }
        )
        global run_id
        run_id = str(response["id"])

Sample Code to Update test Cases

def updateTestCase(self, testCaseId, result):
        global run_id
        if result == "pass":
            status = 1
        elif result == "fail":
            status = 5
        # else:
        # self.log.failed("Unknows Status ID for TestRail test update. Please use 'pass' or 'fail' for the tests.")
        print(testCaseId)
        print(run_id)
        response = client.send_post(
            'add_result_for_case/' + run_id + '/' + testCaseId,
            {
                'status_id': status,
                'comment': 'Test Executed - Status updated from Automated Smoke Test Suite'
            }

Leave a Reply