TestNG and JUnit in one project with maven surefire

This can be achieved using options such as using different profiles for both and another is by setting JUnit property true for surefire plug in.

Run "mvn test". Only the TestNG test will run since as soon as the maven founds the TestNG dependency in main profile it executes TestNG tests only. If you modify the pom to set the property "junit=true", only the JUnit test will run.


org.apache.maven.plugins
maven-surefire-plugin
2.4.2



junit
true



  

But personally my experience is it wont works with JUnit4.

Since it is the TestNG dependency that triggers surefire to use the TestNG runner to execute tests, We've to move this dependency out of the main project scope. In order to compile and run all JUnit tests, and needs to exclude the TestNG tests from the compiler and surefire plugins.

Then in a profile, add TestNG dependency and adjust the compiler and surefire plugins to include the TestNG tests but don't forget and override exclude if you are inheriting from main profile.


test



org.apache.maven.plugins
maven-compiler-plugin
2.0.2

1.5
1.5

**/testNGTests/**.java








testNG



org.apache.maven.plugins
maven-compiler-plugin
2.0.2

1.5
1.5

**/junitTests/**.java







org.testng
testng
5.8
test
jdk15





To run JUnit tests I use: mvn test
To run testNG tests I use: mvn test -P testNG


About this entry


0 comments: