Personal Programming Notes

To err is human; to debug, divine.

Running Maven Tests in Jenkins

A few notes on executing Maven + TestNG tests in Jenkins.

Running tests

Use the following Maven commands in Jenkins when executing tests.

Running tests
1
mvn -f pom.xml clean test

In pom.xml, the test suite file should be specified, such as testng.xml. To run a custom TestNG test suite file, use the following command:

Custom TestNG test suite
1
mvn clean test -DsuiteXmlFile=testng.xml

To run a specific TestNG class, use the following command:

Run specific TestNG class/method
1
2
3
mvn clean test -Dtest=TestCircle

mvn clean test -Dtest=TestCircle#test_area

Note that the symbol # must be used between class name and method name. In other words, -Dtest=TestCircle.test_area will not work.

Links

  1. Custom TestNG suite file
  2. Select method in test class

Configuring tests

Sometimes, your tests need to access resources outside of standard Eclipse/Maven project folders. For the tests to pass both locally and on Jenkins, the full path to the current workspace may be required to resolve the file path to those resources in tests. Jenkins set a number of environment variables, including the variable WORKSPACE as the absolute path of the workspace.

For local environment, we need to set environment variable WORKSPACE to simulate Jenkins environment. In the tests, we need to retrieve this environment variable, as follows in Java:

Getting environment variable
1
String wsPath = System.getenv("WORKSPACE");

Besides workspace’s absolute path, we might also need to configure some constants in tests from a configuration file. When initializing constants from properties files in Maven, remember:

  • Use this.class.getResourceAsStream() method.
  • By Maven convention, properties file should be in src/main/resources folder.
  • Default property values could be used when reading properties file.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static final String CONFIG_PATH = "/config.properties";

static {
    Properties prop = new Properties();
    try ( InputStream stream = VerticaConnection.class.getResourceAsStream(MyConstants.CONFIG_PATH) ) {
        prop.load(stream);

        // Set values from config file with default values
        UNIT_TEST_SCHEMA = prop.getProperty("schema", "UNIT_TEST");
        FUNC_TEST_SCHEMA = prop.getProperty("schema", "FUNC_TEST");
        VSQL_PATH = prop.getProperty("vsql_path", "/opt/vertica/bin/vsql");

    } catch (FileNotFoundException e) {
        System.err.println("Cannot find file " + MyConstants.CONFIG_PATH);
        throw new IllegalStateException("Could not init class VerticaConnection.", e);
    } catch (IOException e) {
        System.err.println("Error reading file " + MyConstants.CONFIG_PATH);
        throw new IllegalStateException("Could not init class VerticaConnection.", e);
    }
}

Note that the location of the properties file in the example above is src/main/resources/config.properties.