To execute TestNG tests from ant task, following are the two steps.
- Define taskdef to add a TestNG task definition to the project, the implementation of the task available in testng.jar.
- Define testng task with below attributes:
- workingDir: source code path to execute TestNG tests.
- outputDir : stores generated reports.
- classpathref : reference to the libraries and class files for the tests to be executed.
- haltOnfailure : stop the build process if a failure has occurred during the test run.
- xmlfileset : include the TestNG xml file for execution.
Example to execute TestNG tests from Ant task
To execute tests from ant task, here are the prerequistes.
- Create a sample TestNG class that has tests
- Create a testng.xml that has classes needs to be executed
- Build.xml file with testng ant task
- Execute testng ant task
Create a sample TestNG class
SAMPLE Testng.xml file
Created a testng.xml file named as sample.xml
Build.xml with ant task
- Created a build.xml with testng ant task target named as ‘run-tests’.
- Target ‘run-tests’ depends on ‘compile’ to compile the java code.
- Target ‘compile’ depends on ‘init’ to delete and create bin directory.
- Below is the complete build.xml file, you can directly use by just copy it.
It has a target ‘ant-tests‘ (<target name=”run-tests” depends=”compile”>) to run tests inside the classes that are given in testng.xml file.
In this target, mentioned ‘outputDir’ to store generated TestNG Reports.
Execute testng ant task
As discussed in previous Apache Ant Build Tool post, here is the command to execute ant targets, ant <targetname>
- ant run-test
When executed target, it compiles the java code and then executes the tests in SampleTests class.
Below is the output:
Here is the project details for more details.
Some times while running TestNG tests from ant taks, might run into below issue.
- The problem here is jcommander jar code was missed inside the TestNG jar. If you are using ant, JCommander should be inside testng.jar.
- Here got the testng jar from Maven Central, but it didn’t contain all of TestNG’s dependencies.
- Download the jar directly from the TestNG site resolved the issue, as it contains the dependencies.
Finally, we learned on how to execute TestNG tests from ant task and its details so that we can execute tests from build xml file.