# 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.

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:

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

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

### 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:

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.

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