Junit4 and TestNG in one project with Maven

后端 未结 3 981
傲寒
傲寒 2020-12-21 05:38

To run them together there are few options available but I have chosen using different profiles for Junit and TestNG. But now problem is with excluding and including test c

相关标签:
3条回答
  • 2020-12-21 06:00

    I didn't find any combined solution online for both Surefire and Failsafe. The POM file changes below worked for me.

    • The Surefire trick is done by explicitly specifying the child plugin for both JUnit and TestNG.
    • The Failsafe trick is done by defining two executions, each with an empty configuration, one for JUnit and the other for TestNG.

    Both solutions are hacks that I found online. I think the link to the Surefire trick is in another answer to this question.

    <plugins>
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire.version}</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
                <version>${surefire.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-testng</artifactId>
                <version>${surefire.version}</version>
            </dependency>
        </dependencies>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${failsafe.version}</version>
        <executions>
            <execution>
                <id>test-testng</id>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <testNGArtifactName>none:none</testNGArtifactName>
                </configuration>
            </execution>
            <execution>
                <id>test-junit</id>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <junitArtifactName>none:none</junitArtifactName>
                </configuration>
            </execution>
        </executions>
    </plugin>           
    <!-- ... -->
    </plugins>
    
    0 讨论(0)
  • 2020-12-21 06:03

    The configuration for the compiler plugin excludes the TestNG types. The configuration from the profile is merged with the default configuration, so your effective compiler configuration is:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
        <testIncludes>
          <testInclude>**/tests/**.java</testInclude>
          <testInclude>**/tests/utils/**.*</testInclude>
        </testIncludes>
        <testExcludes>
          <testExclude>**/tests/**.*</testExclude>
          <testExclude>**/tests/utils/**.*</testExclude>
        </testExcludes>
      </configuration>
    </plugin>
    

    This means that your TestNG types aren't ever compiled and therefore aren't run.

    If you specify the <excludes> section in the testNG profile it will override the default excludes and your TestNG types will be compiled and run. I can't remember if it will work with an empty excludes tag (i.e. <excludes/>), you may have to specify something like this to ensure the default configuration is overridden.

        <testExcludes>
          <testExclude>dummy</testExclude>
        </testExcludes>
    
    0 讨论(0)
  • 2020-12-21 06:05

    Simplest solution is like this

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

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

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