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

前端 未结 12 1979
花落未央
花落未央 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:39

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

    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.

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

    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!

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

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

    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

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

    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>
    
    0 讨论(0)
提交回复
热议问题