Junit4和TestNG与Maven在一个项目中 - java

要一起运行它们,几乎没有可用的选项,但是我选择为Junit和TestNG使用不同的配置文件。但是现在的问题是排除和包含测试用例。

由于如果我们在Maven的主项目中添加testNG依赖项,它将跳过所有Junit,因此我决定将其放在单独的配置文件中。所以我使用pom.xml中的以下条目从默认(主要)配置文件中排除了TestNG测试:

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

与surefire插件相同。因此,它可以与主配置文件配合使用,并且仅执行Junit4测试。但是当我使用testNG配置文件时,它甚至不会编译它们也不会执行testNG测试。我正在使用以下配置文件执行它们。

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

有人知道为什么不包括它们并重新编译吗?

参考方案

编译器插件的配置不包含TestNG类型。配置文件中的配置与默认配置合并,因此您有效的编译器配置为:

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

这意味着您的TestNG类型不会被编译,因此不会运行。

如果在testNG配置文件中指定部分,它将覆盖默认的排除项,并且将编译并运行您的TestNG类型。我不记得它是否可以与空的excludes标记一起使用(即),您可能必须指定类似的内容以确保默认配置被覆盖。

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

Maven:将本地依赖项添加到jar - java

我有一个依赖 <dependency> <groupId>de.matthiasmann</groupId> <artifactId>twl</artifactId> <version>1.0.0</version> <scope>system</scope…

Java中的<<或>>>是什么意思? - java

This question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…

Maven:项目构建错误“modelVersion”丢失 - java

我正在尝试创建一个新的Maven项目,但是我从pom.xml中得到了这个错误...有人可以帮我解决问题吗?我是Maven和Eclipse IDE的新手非常感谢。 参考方案 您的pom.xml不完整你需要<?xml version="1.0" encoding="UTF-8"?> <project xm…

菱形运算符<>是否等于<?> - java

我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…

通过Maven编译器插件不会发生有限的包含和排除 - java

我正在使用3.6.0版的maven编译器插件,在此我们只想在特定文件夹中编译一个文件,而在该位置编译所有其他文件。例如:在文件夹应用程序中有14个文件,从那我只希望编译1个文件,但它编译了所有文件,如果我要排除,则它也不起作用。 <sourceDirectory>${basedir}/../src/java</sourceDirectory…