Running unit tests

Running unit and integration tests with ElasTest doesn't require launching beforehand a Sut. The test itself will manage the execution of the tested software. So the only requirment is to create a TJob into a Project and launch it.

Here we will run our JUnit5 Unit Test provided by default in ElasTest, which has a single class with two methods, sum and sub, that receive two numerical values to add or subtract respectively. This test has been developed in Java using JUnit5:

CalcTest class

public class CalcTest extends ElasTestBase {

    private Calc calc;

    static int left = 3;
    static int right = 2;

    @BeforeEach
    public void setup() {
        this.calc = new Calc();
    }

    @Test
    public void sumTest() {
        int expectedResult = 5;
        logger.info("Checking if {} + {} = {}", left, right, expectedResult);
        assertEquals(expectedResult, calc.sum(left, right));
    }

    @Test
    public void subTest() {

        int expectedResult = 1;
        logger.info("Checking if {} + {} = {}", left, right, expectedResult);
        assertEquals(expectedResult, calc.sub(left, right));
    }

}


In addition, as can be seen in the example, this test class extends a class called ElasTestBase which is responsible for printing logs at the beginning and end of each test. These two logs have a specific structure and are used by ElasTest to filter the logs corresponding to each test. We will explain it in more detail below

ElasTestBase class

public class ElasTestBase {

    protected static final Logger logger = LoggerFactory.getLogger(ElasTestBase.class);

    @BeforeEach
    public void logStart(TestInfo testInfo) {
        logger.info("##### Start test: " + testInfo.getTestMethod().get().getName());
    }

    @AfterEach
    public void logEnd(TestInfo testInfo) {
        logger.info("##### Finish test: " + testInfo.getTestMethod().get().getName());
    }

}


To Run "JUnit5 Unit Test" TJob you only need follow these steps:

1. Access your ElasTest dashboard
2. Get into "Unit Tests" project
3. Run the 'JUnit5 Unit Test' TJob
4. Execution screen is open automatically

Our TJob will start running: you will see the test information and log.

Once the test is finished you will see the test results and log. Eventually the test should end succesfully.

IMPORTANT: ElasTest make use of xml results file to get all the test results information.

You can click on the Test Suite to display it and see each of the Test Cases. You can also click on one of them to navigate to the information section of this Test Case, where you can see their logs filtered thanks to the two traces of logs that the class ElasTestBase prints at the beginning and end of each test and that we have commented above. We are going to click on subTest:

You can click too on the View In LogAnalyzer button for navigate to Log Analyzer Tool and view the execution logs:

Or you can click on the View Case In LogAnalyzer button view the specific test execution logs:

If you want to create the TJob yourself, you only need follow these steps:

1. Access your ElasTest dashboard
2. Create a New Project

The Projects serve to organize the TJobs related to each other. You can create a new Project by clicking on the button with the same name. You only need to indicate the name of the project and then click on SAVE button:

Immediately you will be redirected to the project page:

3. Create a new TJob

You can create a new TJob by clicking on the button with the same name:

When a TJob is created, the minimum information that you have to provide is the following:

  • TJob Name: name of the TJob
  • Select a SuT: If your TJob make use of a Software under Test. In this case, none.
  • Environment docker image: the docker image that will host your test. This docker images should contain a client to download your code from your repository hosting service. For example, if your tests are hosted in GitHub and implemented in a Maven project with Java, you need to include a git client, Maven and the Java Development Kit (JDK) in the image.
  • Commands: these are the bash commands to be executed to download the code from a repository and to execute the tests. The specific commands depends on the source code repository type and the technology.

In our case, we will need to insert the following data for the TJob "JUnit5 Unit Test":

  • TJob Name: can be called as you want, but we will call it JUnit5 Unit Test
  • Test Results Path: /demo-projects/unit/junit5-unit-test/target/surefire-reports. This is the complete path where the xml reports of the execution in the container will be saved. We will explain it in more detail below.
  • Select a SuT: None
  • Environment docker image: elastest/test-etm-alpinegitjava (image that contains Git, Maven and Java).
  • Commands:
      git clone https://github.com/elastest/demo-projects;
      cd /demo-projects/unit/junit5-unit-test;
      mvn -B test
    

By clicking on SAVE the TJob will be saved and you will be redirected to the project page again, where you will be able to execute the TJob.

4. Run the TJob from the Project's page

Click on the "Run TJob" button:

ElasTest makes use of xml reports generated by the tests to obtain the results. The Test Results Path is the complete path of the test container where the xml reports of the execution will be saved.

The appearance of an xml report, such as the one generated when running the TJob JUnit5 Unit Test, is the following:

<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="io.elastest.demo.unit.CalcTest" time="0.017" tests="2" errors="0" skipped="0" failures="0">
  <testcase name="subTest" classname="io.elastest.demo.unit.CalcTest" time="0.006"/>
  <testcase name="sumTest" classname="io.elastest.demo.unit.CalcTest" time="0"/>
</testsuite>

There is no official documentation on this xml format but you can find some examples on the internet, such as the following:

Thanks to this file, ElasTest knows information about each of the executed tests, such as the result, the duration or its name.

In order to know which logs belong to each test and to be able to filter them, ElasTest looks for patterns in the logs that indicate the beginning and the end of the test. Both patterns must be printed on each test and have the following form:

  • Start test trace:

      ##### Start test: TESTNAME
    
  • Finish test trace:

      ##### Finish test: TESTNAME
    

Where TESTNAME is the name of the test. The way of obtaining the name of the test depends on the technology in which the test is developed:

  • In the case of JUnit5 Unit Test that we have used as an example above, it is developed in Java with JUnit5. As can be seen in the ElasTestBase code, the name of the test is obtained by means of the testInfo parameter:

    testInfo.getTestMethod().get().getName()

  • We have included other example tests developed with other technologies. You can see it in the More Examples section

The following examples, also offered by default in ElasTest, are implemented with different technologies:

Java JUnit4 Python JS Jasmine
You can view the Source Code in GitHub. This test has been developed in Java using JUnit4.
CalcTest class

public class CalcTest extends ElasTestBase {

    private Calc calc;

    static int left = 3;
    static int right = 2;

    @Before
    public void setup() {
        this.calc = new Calc();
    }

    @Test
    public void sumTest() {
        int expectedResult = 5;
        logger.info("Checking if {} + {} = {}", left, right, expectedResult);
        assertEquals(expectedResult, calc.sum(left, right));
    }

    @Test
    public void subTest() {
        int expectedResult = 1;
        logger.info("Checking if {} + {} = {}", left, right, expectedResult);
        assertEquals(expectedResult, calc.sub(left, right));
    }

}


ElasTestBase class

public class ElasTestBase {

    protected static final Logger logger = LoggerFactory.getLogger(ElasTestBase.class);

    @Rule
    public TestName name = new TestName();

    @Before
    public void logStart() {
        logger.info("##### Start test: " + name.getMethodName());
    }

    @After
    public void logEnd() {
        logger.info("##### Finish test: " + name.getMethodName());
    }

}


TJob Configuration
  • TJob Name: can be called as you want, but we will call it JUnit4 Unit Test
  • Test Results Path: /demo-projects/unit/junit4-unit-test/target/surefire-reports
  • Select a SuT: None
  • Environment docker image: elastest/test-etm-alpinegitjava
  • Commands:
        
        git clone https://github.com/elastest/demo-projects;
        cd /demo-projects/unit/junit4-unit-test;
        mvn -B test
        
    
You can view the Source Code in GitHub. This test has been developed in Python.
TestUnit class


import unittest
import os
import sys
import xmlrunner
import ElasTestBase
from Calc import \*

leftOperand = 3
rightOperand = 2

class TestUnit(ElasTestBase.ElasTestBase):

    def test_sum(self):
        global leftOperand
        global rightOperand
        expected = 5

        print 'Checking if ' + str(leftOperand) + ' + ' + \
            str(rightOperand) + ' = '+str(expected)

        self.assertEqual(sum(leftOperand, rightOperand), expected)

    def test_sub(self):
        global leftOperand
        global rightOperand
        expected = 1

        print 'Checking if ' + str(leftOperand) + ' - ' + \
            str(rightOperand) + ' = '+str(expected)

        self.assertEqual(sub(leftOperand, rightOperand), expected)

if **name** == '**main**':
file_path = './testresults'
if not os.path.exists(file_path):
os.makedirs(file_path)
file_name = file_path + '/results.xml'
with open(file_name, 'wb') as output:
unittest.main(
testRunner=xmlrunner.XMLTestRunner(output=output),
failfast=False, buffer=False, catchbreak=False)


ElasTestBase class

import unittest

class ElasTestBase(unittest.TestCase):
def setUp(self):
print '##### Start test: ' + self.\_testMethodName

    def tearDown(self):
        print '##### Finish test: ' + self._testMethodName


TJob Configuration
  • TJob Name: can be called as you want, but we will call it Python Unit Test
  • Test Results Path: /demo-projects/unit/python-unit-test/testresults
  • Select a SuT: None
  • Environment docker image: elastest/test-etm-alpinegitpython
  • (Alpine image with Git and Python)
  • Commands:
        
        git clone https://github.com/elastest/demo-projects;
        cd /demo-projects/unit/python-unit-test;
        python UnitTest.py;
        
    
You can view the Source Code in GitHub. This test has been developed in Javascript using Jasmine.
unit-test-spec.js

require('./elastest-conf.js');
var calc = require('../calc.js');

leftOperand = 3;
rightOperand = 2;

describe('Unit Test', function() {
it('Sum', function() {
expectedResult = 5;
console.log('Checking if ' + leftOperand + ' + ' + rightOperand + ' = ' + expectedResult);
expect(calc.sum(leftOperand, rightOperand)).toEqual(expectedResult);
});

    it('Sub', function() {
        expectedResult = 1;
        console.log('Checking if ' + leftOperand + ' - ' + rightOperand + ' = ' + expectedResult);
        expect(calc.sub(leftOperand, rightOperand)).toEqual(expectedResult);
    });

});


elastest-conf.js

var jasmineReporters = require('jasmine-reporters');

jasmine.getEnv().addReporter(
new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: 'testresults',
filePrefix: 'xml-report',
}),
);

var reporterCurrentSpec = {
specStarted: function(result) {
console.log('##### Start test: ' + result.description);
},
specDone: function(result) {
console.log('##### Finish test: ' + result.description);
},
};

jasmine.getEnv().addReporter(reporterCurrentSpec);


TJob Configuration
  • TJob Name: can be called as you want, but we will call it Jasmine Unit Test
  • Test Results Path: /demo-projects/unit/jasmine-unit-test/testresults
  • Select a SuT: None
  • Environment docker image: elastest/test-etm-alpinegitnode
  • (Alpine image with Git, Node, and necessary libraries like jasmine)
  • Commands:
        
        git clone https://github.com/elastest/demo-projects;
        cd /demo-projects/unit/jasmine-unit-test;
        jasmine;