This page gives some guidance on how to use ScriptRunner to work with Test Management for Jira, to accomplish some basic scripting requirements, such as:

Let’s dive straight into the examples.

These examples only work with Test Management for Jira 4.5.3 and above.

Examples

Workflow Condition that prevents an issue transition

The below examples show two ways of preventing an issue transition via a workflow condition using Script Runner’s scripted condition option.

The first example uses a JQL function provided by Test Management for Jira.

The second example shows the same functionality, but this time using Test Management for Jira’s Java API. Using the Java API means that this example can be extended using additional logic.

Please note that in both examples, if an Epic issue type has an associated child issue with a non-passing test, the condition will also fail to pass.

JQL query that fails if the issue has any outstanding non-passing tests

First select the JQL query option for the scripted Workflow Condition to be created.

select jql condition

Now fill in the required details. In this case using the hasAllLastTestResults("Pass") JQL function provided by Test Management for Jira.

add jql condition details

For further details on additional JQL functions provided by Test Management for Jira and how to use them, please see the JQL function documentation.

Script that fails if the issue has any outstanding non-passing tests

First select the Custom script condition option for the scripted Workflow Condition to be created.

select custom script condition

Now fill in the required details. In this case either selecting a script file that contains the below code, or pasting the below code into the inline script box provided.

package examples.docs.tm4j

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.kanoah.testmanager.service.publicservice.IssueLinkPublicService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//Must use @WithPlugin annotation to pick up Test Management for JIRA classes
@WithPlugin("com.kanoah.test-manager")

def issueLinkPublicService = ComponentAccessor.getOSGiComponentInstanceOfType(IssueLinkPublicService)

def checkTestStatus = { Issue targetIssue ->
    issueLinkPublicService.getTestCases(targetIssue.key, "lastTestResultStatus").each {
        if (it.get("lastTestResultStatus") != "Pass") {
            passesCondition = false
        }
    }
}

if (issue.issueType.name == "Epic") {
    ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.id).each {
        if (it.issueLinkType.name == "Epic-Story Link") {
            checkTestStatus(it.destinationObject)
        }
    }
} else {
    checkTestStatus(issue)
}

Please note that the objects returned by the Test Management for Jira Java API are key/value maps representing each object. Or lists of such maps.

You can tailor the key/values returned in the map by specifying the keys as a comma separated parameter value on the method call. In the above example we are requesting that the lastTestResultStatus for each test case be returned.

For further details on the structure of the returned key/value maps provided by Test Management for JIRA, please see the REST API documentation.

Workflow Post Function that creates a test case

The below example shows how to use a scripted post function to create a new test case using details from the issue.

In order to acheive this, go to a transition you wish to attach this functionality to and create a new custom scripted post function.

For the script itself, either select a script file that contains the below code, or paste the below code into the inline script box provided.

Once done, the below code will create a new test case with the issue’s details every time the issue is transitioned with the post function’s corresponding workflow transition.

custom scripted post function
package examples.docs.tm4j

import com.atlassian.jira.component.ComponentAccessor
import com.kanoah.testmanager.model.activeobjects.TestScriptEntity
import com.kanoah.testmanager.service.model.StepDTO
import com.kanoah.testmanager.service.model.TestCaseDTO
import com.kanoah.testmanager.service.model.TestScriptDTO
import com.kanoah.testmanager.service.publicservice.TestCasePublicService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//Must use @WithPlugin annotation to pick up Test Management for JIRA classes
@WithPlugin("com.kanoah.test-manager")

def testCasePublicService = ComponentAccessor.getOSGiComponentInstanceOfType(TestCasePublicService)

def newTestCase = new TestCaseDTO(
    name: issue.summary,
    projectKey: issue.projectObject.key,
    objective: issue.description,
    testScript: new TestScriptDTO(
        type: TestScriptEntity.Type.STEP_BY_STEP.toString(),
        steps: [
            new StepDTO(
                description: "This is the first test step",
                testData: "First test step test data",
                expectedResult: "First test step expected result"
            ),
            new StepDTO(
                description: "This is the second test step",
                testData: "Second test step test data",
                expectedResult: "Second test step expected result"
            )
        ]
    ),
    issueLinks: [issue.key]
)

testCasePublicService.createTestCase(newTestCase)

Scripted Field for displaying the percentage of passing tests

The below example shows the creation of a scripted field which is used to display the percentage of passing tests.

The script will either display the percentage of passing tests for an issue (or 100% in the case of issues with no tests) or, for an Epic issue type, display the percentage of passing tests across all issues within that Epic.

To achieve this, first create a new custom script field using ScriptRunner, with the following configuration:

Set the template option to "Custom". This now allows us to control how we want to display the calculated field. In this case type $value% into the Custom Template text box.

$value represents the calculated result. While % is obviously the unit of measure we want to display in this example.

For the script itself, either select a script file that contains the below code, or paste the below code into the inline script box provided.

create passing tests custom field
package examples.docs.tm4j

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.kanoah.testmanager.service.publicservice.IssueLinkPublicService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//Must use @WithPlugin annotation to pick up Test Management for JIRA classes
@WithPlugin("com.kanoah.test-manager")

def numberOfTests = 0
def numberOfPassingTests = 0

def issueLinkPublicService = ComponentAccessor.getOSGiComponentInstanceOfType(IssueLinkPublicService)

def countTestsInIssue = { Issue targetIssue ->
    issueLinkPublicService.getTestCases(targetIssue.key, "lastTestResultStatus").each {
        numberOfTests++
        if (it.get("lastTestResultStatus") == "Pass") {
            numberOfPassingTests++
        }
    }
}

if (issue.issueType.name == "Epic") {
    ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.id).each {
        if (it.issueLinkType.name == "Epic-Story Link") {
            countTestsInIssue(it.destinationObject)
        }
    }
} else {
    countTestsInIssue(issue)
}

if (numberOfTests == 0) {
    return 100
}
return (double)(numberOfPassingTests * 100) / numberOfTests

Once you have added the scripted field, you should have the below scripted field configuration.

resulting custom field config

This now works. I.e. it displays the percentage of passing tests for an issue. But we can make it even better.

Notice that the Searcher is listed as being a "Free Text Searcher". This is the default.

However, in order to make our scripted field more versatile. I.e. in order to run issue searches such as "Percentage Of Tests Passing < 50" we need to change this to a "Number Searcher". Now your config should look like the one below.

edited custom field config

For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.

Ask a question about ScriptRunner for JIRA, for for Bitbucket Server, or for Confluence.