如何在pom.xml中配置TestNG测试方法组

时间:2013-10-24 06:46:28

标签: maven testng pom.xml

我能够运行所有测试,但我不知道如何在pom.xml中配置组并使用maven运行测试组。

我正在使用TestNG框架工作,但没有添加像pom.xml中的testing.xml。

任何人都可以帮助我使用testng在pom.xml中创建组。下面是我的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.automation.tests</groupId>
    <artifactId>autotest</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>autotest</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <classifier>jdk15</classifier>
            <version>5.11</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>LATEST</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>selenium-maven-plugin</artifactId>
                <!--<executions> <execution> <phase>pre-integration-test</phase> <goals> 
                    <goal>start-server</goal> </goals> <configuration> <background>true</background> 
                    </configuration> </execution> <execution> <id>stop-selenium</id> <phase>post-integration-test</phase> 
                    <goals> <goal>stop-server</goal> </goals> </execution> </executions> -->
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- Skip the normal tests, we'll run them in the integration-test phase -->
                    <skip>true</skip>
                </configuration>

                <executions>
                <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>


    </build>
</project>

2 个答案:

答案 0 :(得分:1)

我通常做的是定义一个我想在src/test/resources/testng文件夹下运行的测试用例,所以假设你有

  • 的src /测试/资源/ TestNG的/ testSuite1.xml
  • 的src /测试/资源/ TestNG的/ testSuite2.xml

现在,您可以使用像

这样的简单命令来运行这些套装
mvn verify -Dtestng.suite.xml=src/test/resources/testng/testSuite1.xml

示例TestNG文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="seleniumTest">

<test name="All Components">
    <groups>
        <run>
            <include name="Some group" />
        </run>
    </groups>

    <packages>
        <package name="org.package" />
        <package name="org.package2" />
    </packages>
</test>

答案 1 :(得分:0)

如果您在案例中定义了组,并且想要在pom中指定组,则可以在pom中执行以下操作。默认情况下,测试阶段使用surefire插件,但您可以明确定义以下内容以使您的组

<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
      <groups>functest,perftest</groups>
    </configuration>
  </plugin>
[...]