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

12

48

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 ?

Fults answered 5/8, 2009 at 12:11 Comment(0)
S
11

There is an open issue for this, so there's no elegant way to do this.

It would be far simpler for you to pick a framework and stick with it.

Edit: My previous answer doesn't work because you can't specify dependencies in the execution. I've tried a few approaches, but the best I can manage is to create a profile for the TestNG dependency so you can toggle between TestNG and JUnit testing, there doesn't seem to be a means to run both TestNG and Junit 4 tests.

One other point to note: You can launch your JUnit tests from TestNG, but I think this only works for JUnit 3 tests.

Sightless answered 5/8, 2009 at 12:31 Comment(4)
Of course this won't work because you can't add dependencies at the execution level. You could move the testng dependency into a profile to choose either junit or testng executions, but that's not what you're after. I'll have a look at the options and update if I find anything outSightless
Thanks rich, yeah thats true what you said in comments. I also tried to add property for "Junit" as "true" as described in that issue but doesn't helped me lot so decided to ask here. Thanks anyways :)Fults
yeah but unfortunately not running for me please refere #1238517Fults
Update: That issue is fixed and closed issues.apache.org/jira/browse/SUREFIRE-377Javierjavler
Z
56

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>
Zoophyte answered 12/11, 2013 at 12:3 Comment(1)
This should go up. The only problem is that if you have a multi-module project, providers as plugin dependencies are NOT inherited. Plugins are inherited, their executions and configs are, but NOT their dependencies. Aside from that, great way to solve problem.Expressionism
H
15

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>
Heaume answered 28/3, 2010 at 22:26 Comment(4)
this solution works except that it launches the surefire plugin 3 times for me: the normal execution (that launches testng)+ 1 one for testng tests only and 1 for junit tests only.Arzola
down vote because of 3 times of runnning. imho the best is to configure the default execution with "<testNGArtifactName>none:none</testNGArtifactName>" see jira.codehaus.org/browse/SUREFIRE-377 and James Kato's comment - works great for meCallboard
@Callboard Well, that one was based on my original idea.Heaume
to disable ? <configuration> <junitArtifactName>nonexisting</junitArtifactName> </configuration>Sensation
S
11

There is an open issue for this, so there's no elegant way to do this.

It would be far simpler for you to pick a framework and stick with it.

Edit: My previous answer doesn't work because you can't specify dependencies in the execution. I've tried a few approaches, but the best I can manage is to create a profile for the TestNG dependency so you can toggle between TestNG and JUnit testing, there doesn't seem to be a means to run both TestNG and Junit 4 tests.

One other point to note: You can launch your JUnit tests from TestNG, but I think this only works for JUnit 3 tests.

Sightless answered 5/8, 2009 at 12:31 Comment(4)
Of course this won't work because you can't add dependencies at the execution level. You could move the testng dependency into a profile to choose either junit or testng executions, but that's not what you're after. I'll have a look at the options and update if I find anything outSightless
Thanks rich, yeah thats true what you said in comments. I also tried to add property for "Junit" as "true" as described in that issue but doesn't helped me lot so decided to ask here. Thanks anyways :)Fults
yeah but unfortunately not running for me please refere #1238517Fults
Update: That issue is fixed and closed issues.apache.org/jira/browse/SUREFIRE-377Javierjavler
V
7

There is another wayout for this. You could ask TestNG to run Junit test cases as well. Below is the sample testng.xml to run all test cases

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 
<suite name="TestAll">
 
    <test name="junitTestCases" junit="true">
        <packages>
            <package name="com.test.*" />
        </packages>
    </test>
 
    <test name="testNGTestCases" >
        <packages>
            <package name="com.test.*" />
        </packages>
    </test>
</suite>
Vaenfila answered 23/3, 2015 at 9:25 Comment(2)
This works, although the grouping of results comes out a bit strange.Lonnie
Be aware, that TestNG seems to ignore simply problematic tests without saying anything. For example I had a rule (which has to be a public field), which was private, then testng simply ignored the test without saying anything.Bethea
A
6

Thanks to this link (http://jira.codehaus.org/browse/SUREFIRE-377), here is a solution to my previous problem (having 3 executions instead of 2)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
       <testNGArtifactName>none:none</testNGArtifactName>
    </configuration>
    <executions>
       <execution>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
             <junitArtifactName>none:none</junitArtifactName>
             <testNGArtifactName>org.testng:testng</testNGArtifactName>
          </configuration>
       </execution>
    </executions>
</plugin>
Arzola answered 29/10, 2011 at 17:26 Comment(1)
Not enough though, because if you want to run a single test using -Dtest=XXX , it may fail depending on the main provider. For instance, as junit is my main provider, trying to run any TestNG test using -Dtest=XXX would fail.Arzola
C
3

I found out a solution to run both test types with TestNG without changing your build tool configuration.

I tested with Gradle but should work with Maven too.

Note that this will run JUnit tests inside TestNG, but not the other way back.

The trick is to use both frameworks' annotations in the test classes and use TestNG asserts for JUnit compatibility.

import static org.testng.AssertJUnit.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

@org.testng.annotations.Test
public final class ApplicationTest {

    @org.testng.annotations.BeforeClass
    @BeforeClass
    public static void setup () {}

    @org.testng.annotations.AfterClass
    @AfterClass
    public static void cleanup () {}

    @Test public void json () throws IOException {
        assertTrue (true);
    }
}

Using this hack, you can easily run existing JUnit tests with TestNG, helping you migrate them when time allows.

Hope it helps!

Choking answered 10/2, 2015 at 20:58 Comment(0)
S
2

For JUnit ---

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

Similarly use the dependency for TestNG when required

Shutt answered 4/1, 2017 at 22:33 Comment(0)
D
1

I found that the solution was to force the sure-fire plugin to use JUnit. I did this by overriding surefire plugin in the specific project as follows. The dependency forces surefire to use JUnit.

<build>
    <plugins>
        <!-- force sure fire to use junit instead of testng -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.10</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
Disharmonious answered 18/10, 2011 at 11:17 Comment(1)
This answer is kinda made obsolete by MariuszS answer above (the official way). Also, it did not fire up TestNG tests (at least under Surefire 2.10 and 2.16).Expressionism
D
1

Based on previous solutions. I found this worked best for us. One more issue we were facing was TestNG trying to run old JUnit tests. We avoided this by naming all TestNG tests differently (e.g. *TestNG.java). Below is the configuration with two executions of surefire-plugin.

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>           
                <testNGArtifactName>none:none</testNGArtifactName>   
                <excludes>
                    <exclude>**/*TestNG.java</exclude>
                </excludes> 
            </configuration>
            <executions>
                <execution>
                    <id>test-testng</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration> 
                        <junitArtifactName>none:none</junitArtifactName>          
                        <testNGArtifactName>org.testng:testng</testNGArtifactName>   
                        <includes>
                            <include>**/*TestNG.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*Test.java</exclude>
                        </excludes> 
                    </configuration>
                </execution>
            </executions>
        </plugin>
Dineric answered 11/12, 2012 at 23:36 Comment(0)
C
1

if you just specify testng provider, it will run both junit tests and testng tests all just once.
so there is no restriction on naming the tests.

plugin versions:
surefire-plugin 2.16 (junit47 and testng providers both version set to 2.16)
testng dependency 6.8.7
junit dependency 4.7

Cystine answered 11/2, 2014 at 20:6 Comment(1)
I just migrated project from JUnit to test NG and realized there are no compilation problems, anything junit:junit:jar:4.12 -> org.testng:testng:jar:7.3.0Isomorph
C
1
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <!-- ********* Skip Test for Success BUILD ******** -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <!-- *********************************************** -->
    </plugins>
</build>

<profiles>
    <!-- ********** Profiles for run test cases ************ -->
    <!-- Profile for run JUnit test dependent tests -->
    <profile>
        <id>junit-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.10</version>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>2.10</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- Profile for run TestNG dependent tests -->
    <profile>
        <id>testNG-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.10</version>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- ***************************************************** -->
</profiles>

Than just run: mvn test -Pjunit-tests (for run test based on junit) or mvn test -PtestNG-tests (for TestNG test based).

Coming answered 8/5, 2014 at 11:33 Comment(0)
F
1

for Junit this solved my problem

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>2.19.1</version>
  </dependency>
</dependencies>
Fluidize answered 12/11, 2017 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.