Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?

Right now I have both type of tests but when I say "mvn test" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ?

like image 996
ravinikam Avatar asked Aug 05 '09 12:08

ravinikam


People also ask

Can I use both JUnit and TestNG together?

You can execute your existing JUnit test cases using TestNG. TestNG can automatically recognize and run JUnit tests, so that you can use TestNG as a runner for all your existing tests and write new tests using TestNG.

How do I run Maven surefire plugin?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

Which is the Maven plugin for running JUnit tests?

The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.

How can you use Maven to run unit tests in parallel?

Maven Dependencies In a nutshell, Surefire provides two ways of executing tests in parallel: Multithreading inside a single JVM process. Forking multiple JVM processes.


2 Answers

Official way with selecting providers.

You can also specify multiple providers as dependencies, and they will all be run and produce a common report. This may be especially handy with external providers, since there are few use-cases for combining the included providers.

<plugin>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.18.1</version>     <dependencies>         <dependency>             <groupId>org.apache.maven.surefire</groupId>             <artifactId>surefire-junit47</artifactId>             <version>2.18.1</version>         </dependency>         <dependency>             <groupId>org.apache.maven.surefire</groupId>             <artifactId>surefire-testng</artifactId>             <version>2.18.1</version>         </dependency>     </dependencies> </plugin> 

More info about this: Mixing TestNG and JUnit tests in one Maven module – 2013 edition

Current Link for this in the maven-surefire-plugin examples. Search for "Running TestNG and JUnit Tests".

You will want to configure the testng provider to ignore the junit tests like so:

<plugin>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.18.1</version>     <configuration>             <properties>         <property>             <name>junit</name>             <value>false</value>          </property>     </properties>             </configuration>     [...providers as dependecies, see above...] </plugin> 
like image 188
MariuszS Avatar answered Sep 22 '22 12:09

MariuszS


I have a better solution.

The idea is to create two executions of the maven-surefire-plugin, one for JUnit, one for TestNG. You can disable one of TestNG or JUnit per execution by specifying nonexisting junitArtifactName or testNGArtifactName:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-surefire-plugin</artifactId>     <executions>         <execution>             <phase>test</phase>             <goals>                 <goal>test</goal>             </goals>             <configuration>                  <testNGArtifactName>none:none</testNGArtifactName>             </configuration>         </execution>         <execution>             <id>test-testng</id>             <phase>test</phase>             <goals>                 <goal>test</goal>             </goals>             <configuration>                  <junitArtifactName>none:none</junitArtifactName>             </configuration>         </execution>     </executions> </plugin> 
like image 34
lexicore Avatar answered Sep 22 '22 12:09

lexicore