Goals
- Get all issues of given Filter
- Get custom field from every Test Case
- Maintain a Map of JIRA ID to Custom Field
- Create a TestNG XML suite Programmatically and run it
- Get results programmatically from TestNG
- Update the issues by using Reverse mapping of Custom Field to JIRA ID
How to Achieve
- Create a Filter
- project = MFS AND issuetype = "Test Case" AND "Test Cycle" = "Version 4.0 - Cycle 1" AND "Automation Status" = Automated AND status = "In Progress"
- Build jar from the code at the end of this Post and run on Jenkins.
- This code needs following inputs
- JIRA URL
- JIRA Credentials
- Filter like “filter=10112”
- customField="customfield_10032"
- Note: You can get custom field name from JIRA Rest API and this name is different from what you see in JIRA UI
package com.company.qa.project.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import net.rcarz.jiraclient.BasicCredentials;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;
public class JIRATest implements IReporter{
ArrayList<String> failedTests = new ArrayList<String>();
Collection<String> passedTests = new ArrayList<String>();
Collection<String> skippedTests = new ArrayList<String>();
BasicCredentials creds = new BasicCredentials("username@mail.com", "password");
JiraClient jira = new JiraClient("https://domain.atlassian.net", creds);
String filter = "filter=10112";
String customField = "customfield_10032";
public static void main(String[] args) {
JIRATest jiraTest = new JIRATest();
jiraTest.runTests();
}
public void runTests(){
TestNG testNG = new TestNG();
XmlSuite suite = new XmlSuite();
suite.setName( "MFS Automated Suite" );
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName( "MFS Autmation Test" );
xmlTest.setVerbose( 0 );
List<Class> ListenerClasses = new ArrayList<Class>();
ListenerClasses.add(com.company.qa.project.test.JIRATest.class);
testNG.setListenerClasses(ListenerClasses);
try {
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
/* Retrieve issue TEST-123 from JIRA. We'll get an exception if this fails. */
Issue.SearchResult sr = jira.searchIssues(filter);
System.out.println("Total number of Tests to be run: " + sr.total);
for (Issue i : sr.issues){
/* Pretend customfield_1234 is a text field. Get the raw field value... */
Object cfvalue = i.getField(customField);
/* ... Convert it to a string and then print the value. */
String[] cfstrings = Field.getString(cfvalue).trim().split("#");
if(cfstrings.length == 2){
ArrayList<String> methods = map.get(cfstrings[0]);
if(null == methods){
methods = new ArrayList<String>();
methods.add(cfstrings[1]);
}else{
methods.add(cfstrings[1]);
}
map.put(cfstrings[0], methods);
}
}
for (Map.Entry<String,ArrayList<String>> entry : map.entrySet()) {
String key = entry.getKey();
ArrayList<String> methods = entry.getValue();
XmlClass xmlClass = new XmlClass(key);
xmlTest.getClasses().add(xmlClass);
for(String method: methods){
XmlInclude xmlInclude = new XmlInclude(method);
xmlClass.getIncludedMethods().add(xmlInclude);
}
}
System.out.println("Running Tests using command ..");
List<XmlTest> tests = suite.getTests();
for(XmlTest test: tests){
List<XmlClass> classes = test.getClasses();
for(XmlClass xmlClass: classes){
List<XmlInclude> methods = xmlClass.getIncludedMethods();
for(XmlInclude xmlInclude : methods){
System.out.println("Class: "+xmlClass.getName()+" Method: "+xmlInclude.getName() );
}
}
}
testNG.setXmlSuites( Arrays.asList( suite ) );
testNG.setUseDefaultListeners( false );
testNG.run();
} catch (JiraException ex) {
System.err.println(ex.getMessage());
if (ex.getCause() != null)
System.err.println(ex.getCause().getMessage());
}
}
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
//Iterating over each suite included in the test
try{
HashMap<String,String> mapOfTCAndMethods = new HashMap<String,String>();
Issue.SearchResult srlt = jira.searchIssues(filter);
System.out.println("Total number of Tests to be run: " + srlt.total);
for (Issue i : srlt.issues){
/* Pretend customfield_1234 is a text field. Get the raw field value... */
Object cfvalue = i.getField(customField);
/* ... Convert it to a string and then print the value. */
String[] cfstrings = Field.getString(cfvalue).trim().split("#");
if(cfstrings.length == 2){
mapOfTCAndMethods.put(Field.getString(cfvalue).trim(),i.getKey());
}
}
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map<String,ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName +
"' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Passed Tests : ");
for (ITestResult s : tc.getPassedTests().getAllResults()) {
System.out.println("Marking "+mapOfTCAndMethods.get(s.getTestClass().getName()+"#"+s.getMethod().getMethodName())+" as Passed ...");
Issue issue = jira.getIssue(mapOfTCAndMethods.get(s.getTestClass().getName()+"#"+s.getMethod().getMethodName()));
issue.transition().execute("Pass");
}
System.out.println("Failed Tests : ");
for (ITestResult s : tc.getFailedTests().getAllResults()) {
System.out.println("Marking "+mapOfTCAndMethods.get(s.getTestClass().getName()+"#"+s.getMethod().getMethodName())+" as Failed ...");
Issue issue = jira.getIssue(mapOfTCAndMethods.get(s.getTestClass().getName()+"#"+s.getMethod().getMethodName()));
issue.transition().execute("Fail");
}
System.out.println("Failed tests for suite '" + suiteName +
"' is:" +
tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName +
"' is:" +
tc.getSkippedTests().getAllResults().size());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
~Yagna