Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This recipe could also be applied for other frameworks such as NUnit or Robot.

...

languagejs
title.gitlab-ci.yml

...

We need to setup a Git repository containing the code along with the configuration for GitLab build process.


The tests are implemented in a JUnit class as follows.

...

Code Block
languagejava
titleCalcTest.java
collapsetrue
package com.xpand.java;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class CalcTest {

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }

	@Test
    public void CanAddNumbers()
    {
        assertThat(Calculator.Add(1, 1), is(2));
        assertThat(Calculator.Add(-1, 1), is(0));
    }


    @Test
    public void CanSubtract()
    {
        assertThat(Calculator.Subtract(1, 1), is(0));
        assertThat(Calculator.Subtract(-1, -1), is(0));
        assertThat(Calculator.Subtract(100, 5), is(95));
    }


    @Test
    public void CanMultiply()
    {
        assertThat(Calculator.Multiply(1, 1), is(1));
        assertThat(Calculator.Multiply(-1, -1), is(1));
        assertThat(Calculator.Multiply(100, 5), is(500));
    }


    public void CanDivide()
    {
        assertThat(Calculator.Divide(1, 1), is(1));
        assertThat(Calculator.Divide(-1, -1), is(1));
        assertThat(Calculator.Divide(100, 5), is(20));
    }


    @Test
    public void CanDoStuff()
    {
        assertThat(true, is(true));
    }


}


The GitLab configuration file .gitlab-ci.yml contains the definition of the build steps, including running the automated tests and submitting the results.

Code Block
languagejs
title.gitlab-ci.yml
# Use Maven 3.5 and JDK8
image: maven:3.5-jdk-8

variables:
  # This will supress any download for dependencies and plugins or upload messages which would clutter the console log.
  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  # As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
  # when running from the command line.
  # `installAtEnd` and `deployAtEnd`are only effective with recent version of the corresponding plugins.
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"


# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_REF_NAME"'
cache:
  paths:
    - .m2/repository


maven_build:
  script:
    - echo "building my amazing repo..."
    - mvn test
    - 'curl -H "Content-Type: multipart/form-data" -u $jira_user:$jira_password -F "file=@target/surefire-reports/TEST-com.xpand.java.CalcTest.xml" "$jira_server_url/rest/raven/1.0/import/execution/junit?projectKey=CALC"'
    - echo "done"


In order to submit those results, we'll just need to invoke the REST API (as detailed in Import Execution Results - REST).

Run automated tests

Our project is Maven based, therefore the first Build Step compiles and runs the JUnit automated tests.

Image Removed

Image Removed

Import execution results

However, we do not want to have the JIRA credentiails hardcoded in GitLab's configuration file. Therefore, we'll use some secret variables defined in GitLab project settings.


Image Added


In .gitlab-ci.yml a "step" must be included in the maven_build section, that will use "curl" in In order to submit the results , we'll need to add a Build Step of type "Command Line", where we'll invoke the REST API, submitting the JUnit XML report generated in the previous step.

Image Removed

The complete script content of the "custom script" field above is:

...

curl -H "Content-Type: multipart/form-data" -u 

...

$jira_

...

user:

...

$jira_

...

password -F "file=

...

@target/surefire-reports/TEST-com.xpand.java.CalcTest.xml" "

...

$jira_

...

server_

...

url/rest/raven/1.0/import/execution/junit?projectKey=CALC

...

"


We're using "curl" utility that comes in Unix based OS'es but you can easily use another tool to make the HTTP request.

Notice that we're using some parameters for storing Jira's base URL along with the credentials to be used in the REST API.

Actually, these parameters can be defined at multiple levels; in our example we defined them at the "Build Configuration" level but they could also have been defined at  the project level. 

Image Removed

The parameters can be hidden, such as the password, if you defined them as being of type "Password". 

Image Removed; however, "curl" is provided in the container used by GitLab.



Cucumber example

In this scenario, we are managing the specification of Cucumber Scenarios/Scenario Outline(s) based tests in Jira, as detailed in the "standard workflow" mentioned in Testing with Cucumber

...

You may have noticed a trick in the cucumber line above, in the end of the command (i.e. ".... || :"). That ensures that cucumber returns with exit code 0 (i.e. success), so the build may proceed.

Import execution results

In order to submit the results, we'll need to add a Build Step of type "Command Line", where we'll invoke the REST API, submitting the Cucumber JSON report generated in the previous step.

...