【问题标题】:How to configure TestNG test method group in pom.xml如何在 pom.xml 中配置 TestNG 测试方法组
【发布时间】:2013-10-25 18:26:56
【问题描述】:

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

我正在使用 TestNG 框架,但没有像 pom.xml 中的 testing.xml 那样添加任何内容。

谁能帮我在 pom.xml 中使用 testng.. 下面是我的 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>

【问题讨论】:

    标签: maven testng pom.xml


    【解决方案1】:

    如果您在案例中定义了组,并且您想在 pom 中指定组,那么您可以在 pom.xml 中执行以下操作。默认情况下,测试阶段使用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>
    [...]
    

    【讨论】:

    • 顺便说一句,为什么会有这么旧的Testng版本?
    • 感谢 niharika 的回复。但我在“studentBrowserDriver.get(stURL);”这一行得到空指针 Excedption。如果我在没有组的情况下运行测试,那么它工作正常。 . 知道为什么当我与组一起运行时会出现此错误
    • 要么 Sturl 为空,要么你的 studentbrowserdriver 没有被初始化。需要更多代码来帮助您。初始化驱动的代码在哪里?
    • 我曾经使用 maven 运行测试,但是使用 maven 运行时不会调用“BeforeMethod 和 afterMethods”
    • 嗨,如果我尝试运行“functest”组测试,那么“perftest”组也在运行
    【解决方案2】:

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

    • src/test/resources/testng/testSuite1.xml
    • src/test/resources/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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2013-02-05
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多