简体   繁体   English

Junit4和TestNG在Maven的一个项目中

[英]Junit4 and TestNG in one project with Maven

To run them together there are few options available but I have chosen using different profiles for Junit and TestNG. 要将它们一起运行,可用的选项很少,但我选择了为Junit和TestNG使用不同的配置文件。 But now problem is with excluding and including test cases. 但现在的问题是排除和包括测试用例。

Since if we add testNG dependency to main project in maven it will skip all Junit,I have decided to put it in separate profile. 因为如果我们在maven中将testNG依赖项添加到主项目,它将跳过所有Junit,我已经决定将它放在单独的配置文件中。 So I am excluding TestNG tests in default(main) profile from compiling using following entry in pom.xml : 所以我在默认(主)配置文件中排除TestNG测试,使用pom.xml中的以下条目进行编译:

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

and same for surefire plugin. 和surefire插件相同。 So it works fine with main profile and executes only Junit4 tests. 所以它适用于主配置文件并且只执行Junit4测试。 But when I use testNG profile it wont execute testNG test even it wont compile them. 但是,当我使用testNG配置文件时,它不会执行testNG测试,即使它不会编译它们。 I am using following profile to execute them. 我正在使用以下配置文件来执行它们。

<profile>
    <id>testNG</id>
    <build>
        <plugins>
            <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>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                    <includes>
                        <include>**/**.class</include>
                        <include>**/tests/utils/**.class</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>5.8</version>
            <scope>test</scope>
            <classifier>jdk15</classifier>
        </dependency>
    </dependencies>
</profile>

Anybody have any idea why it is not including them and compiling again ? 任何人都知道为什么它不包括它们并再次编译?

The configuration for the compiler plugin excludes the TestNG types. 编译器插件的配置不包括TestNG类型。 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. 这意味着您的TestNG类型不会被编译,因此不会运行。

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. 如果在testNG配置文件中指定<excludes>部分,它将覆盖默认的排除,并且将编译和运行您的TestNG类型。 I can't remember if it will work with an empty excludes tag (ie <excludes/>), you may have to specify something like this to ensure the default configuration is overridden. 我不记得它是否可以使用空的排除标记(即<excludes />),您可能必须指定类似的内容以确保覆盖默认配置。

    <testExcludes>
      <testExclude>dummy</testExclude>
    </testExcludes>

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 有关此内容的更多信息: 在一个Maven模块 - 2013版中混合TestNG和JUnit测试

I didn't find any combined solution online for both Surefire and Failsafe. 我没有在Surefire和Failsafe上找到任何在线组合解决方案。 The POM file changes below worked for me. 下面的POM文件更改为我工作。

  • The Surefire trick is done by explicitly specifying the child plugin for both JUnit and TestNG. Surefire技巧是通过为JUnit和TestNG显式指定子插件来完成的。
  • The Failsafe trick is done by defining two executions, each with an empty configuration, one for JUnit and the other for TestNG. Failsafe技巧是通过定义两个执行来完成的,每个执行都有一个空配置,一个用于JUnit,另一个用于TestNG。

Both solutions are hacks that I found online. 这两种解决方案都是我在网上找到的黑客。 I think the link to the Surefire trick is in another answer to this question. 我认为Surefire技巧的链接是对这个问题的另一个答案。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM