Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Run ID tracking in Automated Test

#1

Hello Team,

I am able to Integrate Test Rail Api with Test NG and Selenium Webdriver. Now the only doubt I have is like how to track Test Run ID. Is this the only way to track that if I am not Executing the Script from TestRail UI.

1.Go to Test Rail and Create a Test Run.
2.Take the Test Run Id from UI.
3.Give that Test Run Id to My Automation Run(Example: In Some Config File)
4.Track the same Test Run Id and use that with the TestCase ID.

Please suggest If any possible solution for this Problem.
Regards,
Pawan

0 Likes

#2

Hi Pawan,

Thanks for your posting. Most integrations create a new test run/plan as part of the automated tests and then add test results to the new run/plan. An alternative is to reuse the same run/plan over a longer period (e.g. for the entire sprint) and you can then create the run/plan in TestRail and add the ID to a configuration file or to your code as you suggested as well.

Cheers,
Tobias

0 Likes

#3

Hello Tobias,

Thanks for your Reply, I am able to integrate Webdriver/TestNG and TestRail.

I am trying to describe what I did so far and I think this will help other people and I can get feedbacks from Other People about the Implementation.

STEP 1: First Create One new Annotation class

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface UseAsTestRailId
    {
        int testRailId() default 0;
        String[] tags() default "";
    }

STEP 2: Use the above annotation in TestNg Class File with Other Test.
**** Every @Test here is mapped with the Use case In Test Rail So we got the TestRail Unique Id for every Test.****

@Test
@UseAsTestRailId(testRailId=<TestRail Id for this Usecase>)
public void yourTestNumberSomething()
{
  // Do your testing Steps here which are mapped with testRail Case.
}

STEP 3: You can Use TestNG Listner for Every Test Class, So that you can execute the Test Rail API after the test Execution.

@Listeners(com.gurock.testrail.Listener.class)
public class TestIngClass {
    // All the Test Will be here ...
}

STEP 4: You need to Call the test Rail API’s from Listener Class. Like I am Calling the API in the on Test Success Method.

public void onTestSuccess(ITestResult result) {
String TestID=null;
IClass obj = result.getTestClass();
Class newobj = obj.getRealClass();
Method testMethod = newobj.getMethod(result.getName());
if (testMethod.isAnnotationPresent(UseAsTestRailId.class)) 
{
UseAsTestRailId useAsTestName = testMethod.getAnnotation(UseAsTestRailId.class);
// Get the TestCase ID for Test Rail
TestID = Integer.toString(useAsTestName.testRailId());
System.out.println("Test Rail ID = " + TestID);
}

// Now You can call the Test Rail Post API call to update the Result in Test Rail Based on the Test Run Id and TestCase ID.

JSONObject r = (JSONObject) client.sendPost("add_result_for_case/TestCaseID/TestRunID", data);
}

and Its Done …

You can change and twist the code and Logic based on your requirements but i think this might help people in getting started with Integration.

Regards,
Pawan Garia

0 Likes

#4

Thanks for sharing this, Pawan!

Cheers,
Tobias

0 Likes

#5

Hi @pawangaria

how did you get TestRunID?

0 Likes

#6

Hi there,

Thank you for your post. You can get the run ID of a test run by using the get_runs method:

http://docs.gurock.com/testrail-api2/reference-runs#get_runs

You can also use the run ID by using the get_plans method as well if your test run sits inside of a test plan:

http://docs.gurock.com/testrail-api2/reference-plans#get_plan

0 Likes

#7

thank for your help @martylavender

0 Likes

#8

No problem. I am glad I was able to help.

0 Likes

#9

Hi all,
I’m working with TestNg + Allure. I integrated tests like you described above using @TmsLink("") allure annotation but I have one problem. How can I integrate it using Data-driven Test?
Could someone help?

0 Likes

#10

Hi Pawan,

I am integrating the same as above but with minor differences as follows:

  1. My testcases have data provider as my test case run with cross platforms.
  2. Annotation class have Test id as int array because one test case may have covered more that one test cases.

Problem: I am getting exception due to data provider in below code.

Please help if you have solution in this scenario.

int TestID ;
IClass obj = result.getTestClass();
Class newobj = obj.getRealClass();
Method testMethod = newobj.getMethod(result.getName());// getting exception here
if (testMethod.isAnnotationPresent(TestData.class))
{
TestData useAsTestName = testMethod.getAnnotation(TestData.class);
// Get the TestCase ID for TestRail
TestID = useAsTestName.testId();
System.out.println("Test Rail ID = " + TestID);

	for (int testid : useAsTestName.testId()) {
		try {
			addResultForTestCase(testid, 1, result.getParameters().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}
0 Likes