【问题标题】:How to limit TestNG to run just test classes from project test directory如何限制 TestNG 只运行项目测试目录中的测试类
【发布时间】:2018-04-11 19:29:25
【问题描述】:

pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="my-suite">
    <test name="tests">
        <packages>
            <package name="com.example.*"/>
        </packages>
    </test>
</suite>

TestNG 将扫描所有类路径以查找测试类 (PackageUtils # findClassesInPackage)。
不需要的测试来自 test-jar 依赖项。
如何配置TestNG在maven项目测试目录中查找测试类(src/test/java默认编译为target/test-classes)?

【问题讨论】:

    标签: maven testng maven-3


    【解决方案1】:

    首先,如果你的 classes 目录中没有测试,有什么好担心的?

    其次,您可以更改测试类的包,它对搜索也很有用,例如com.example.qa.*.

    第三个 - 您可以尝试使用 maven-surefire-plugin 中的类路径进行操作,请参阅 documentation

    【讨论】:

      【解决方案2】:

      我们使用了一个非常好的设置,其中包含使用 surefire 和故障安全插件的单独单元测试和集成测试。我从不使用 suite.xml 或类似的测试套件定义。您可以将包含和排除与文件模式一起使用来控制执行的内容。我们还使用自定义侦听器,因为我想对测试输出进行一些控制,并且我们同时运行测试,除非您编写不交互的测试,否则这对您不起作用,这始终是个好主意。

               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.19.1</version>
                  <configuration>
                      <excludes>
                          <exclude>**/*IntegrationTest.java</exclude>
                          <exclude>**/*ApiTest.java</exclude>
                      </excludes>
                      <parallel>classes</parallel>
                      <threadCount>20</threadCount>
                      <runOrder>random</runOrder>
                      <properties>
                          <property>
                              <name>listener</name>
                              <value>io.inbot.testutil.TestProgressLogger,io.inbot.testutil.TestNgRandomizerWithSeed</value>
                          </property>
                      </properties>
                      <systemProperties>
                          <property>
                              <!-- this disables expensive loading of spring
                                  context and starting our test server: keep the unit tests fast! any expensive
                                  test should end with IntegrationTest or ApiTest. They will be run separately
                                  by failsafe plugin -->
      
                              <name>nointegrationtests</name>
                              <value>true</value>
                          </property>
                      </systemProperties>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-failsafe-plugin</artifactId>
                  <version>2.19.1</version>
                  <executions>
                      <execution>
                          <goals>
                              <goal>integration-test</goal>
                              <goal>verify</goal>
                          </goals>
                      </execution>
                  </executions>
                  <configuration>
                      <includes>
                          <include>**/*IntegrationTest.java</include>
                          <include>**/*ApiTest.java</include>
                      </includes>
                      <parallel>classes</parallel>
                      <threadCount>4</threadCount>
                      <runOrder>random</runOrder>
                      <argLine>-Xms512m -Xmx1500m</argLine>
                      <properties>
                          <property>
                              <name>listener</name>
                              <value>io.inbot.testutil.TestProgressLogger,io.inbot.testutil.TestNgRandomizerWithSeed</value>
                          </property>
                      </properties>
                      <systemProperties>
                      </systemProperties>
                  </configuration>
              </plugin>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 2018-10-15
        • 2019-05-21
        • 1970-01-01
        相关资源
        最近更新 更多