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

前端 未结 12 1980
花落未央
花落未央 2020-12-13 06:04

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 ?

相关标签:
12条回答
  • 2020-12-13 06:50

    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>
    
    0 讨论(0)
  • 2020-12-13 06:52

    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>
    
    0 讨论(0)
  • 2020-12-13 06:55
    <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).

    0 讨论(0)
  • 2020-12-13 06:58

    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

    0 讨论(0)
  • 2020-12-13 07:00

    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>
    
    0 讨论(0)
  • 2020-12-13 07:03

    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>

    0 讨论(0)
提交回复
热议问题