A few notes on executing Maven + TestNG tests in Jenkins.
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:
1 2 3
Note that the symbol
# must be used between class name and method name.
In other words,
-Dtest=TestCircle.test_area will not work.
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:
- By Maven convention, properties file should be in
- Default property values could be used when reading properties file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Note that the location of the properties file in the example above is