簡體   English   中英

如何讓我的 maven 構建運行從目標\測試類生成的單元測試?

[英]How do I get my maven build to run generated unit tests from target\test-classes?

我使用代碼生成器 JAVA 程序將單元測試源生成到target\generated-test-sources中,我將其成功編譯到target\test-classes中,但它們被 maven 構建忽略。 我用mvn clean test啟動這個構建。

在我的 pom.xml 文件中,我有一個 main class com.mypackage.TestCaseGenerator的執行目標,我綁定到generate-test-sources階段。

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>myexec</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>java</executable>
              <classpathScope>test</classpathScope>
              <arguments>
                <argument>-classpath</argument>
                <classpath/>
                <argument>com.mypackage.TestCaseGenerator</argument>
                <argument>target/generated-test-sources/somedir</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>target/generated-test-sources/somedir</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

當我通過right-click -> run as JUnit Test on target/generated-test-sources/somedir ,測試正確執行。

我是否缺少一些 maven 配置?

如果您生成的測試的類/文件名與 Maven Surefire 插件的默認值不匹配,您將必須配置它以包含其他模式的測試。

有關測試的包含和排除,請參閱官方插件文檔:

默認情況下,Surefire 插件將自動包含具有以下通配符模式的所有測試類:

  • "**/Test*.java" ——包括它的所有子目錄和所有以“Test”開頭的 Java 文件名。
  • "**/*Test.java" ——包括它的所有子目錄和所有以“Test”結尾的 Java 文件名。
  • "**/*Tests.java" ——包括它的所有子目錄和所有以“Tests”結尾的 Java 文件名。
  • "**/*TestCase.java" ——包括它的所有子目錄和所有以“TestCase”結尾的 Java 文件名。

您可以使用插件配置的<includes><excludes>部分配置不同的模式。 您將在上面鏈接的文檔頁面上找到有關如何執行此操作的很好示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM