如何限制TestNG只从项目测试目录运行测试类

时间:2018-04-11 19:29:25

标签: maven testng maven-3

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)中查找测试类?

2 个答案:

答案 0 :(得分:0)

首先,如果您的课程目录中没有测试需要担心什么?

辅助,您可以更改测试类的包,它也可用于搜索,例如com.example.qa。*。

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

答案 1 :(得分:0)

我们使用一个非常好的设置,使用surefire和failsafe插件进行单独的单元测试和集成测试。我从不使用suite.xml或类似的测试套件定义。您可以使用包含和排除文件模式来控制执行的内容。我们还使用自定义监听器,因为我想对testoutput进行一点控制,并且我们同时运行测试,除非你编写不交互的测试,否则这对你不起作用,这总是一个好主意。

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