从 Maven 运行特定的 TestNG 组 [英] Running Specific TestNG Groups from Maven

查看:38
本文介绍了从 Maven 运行特定的 TestNG 组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 组测试用例作为下面的mentioend.

I have 2 groups of test cases as mentioend below.

@Test(groups="one", dataProvider = "TestData")
public void firstTest(String data){
   //Code
}

@Test(groups="one", dataProvider = "TestData")
public void secondTest(String data){
   //Code
}

@Test(groups="two", dataProvider = "TestData")
public void thirdTest(String data){
   //Code
}

下面是 XML 文件.

Below is the XML file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
    <test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
        <listeners>
             <listener class-name="GroupByInstanceEnabler"></listener>
        </listeners>

        <classes>
            <class name="SampleTest">
                <methods>
                    <include name="firstTest"/>
                    <include name="secondTest"/>
                    <include name="thirdTest"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

下面是 pom.xml 构建细节.

Below is the pom.xml build details.

 <build>
        <finalName>Automation</finalName>
        <filters>
            <filter>profiles/${build.profile.id}/config.properties</filter>
        </filters>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/test/resources</directory>
            </resource>
        </resources>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${project.basedir}/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <parallel>method</parallel>
                    <threadCount>2</threadCount>
                </configuration>
            </plugin>

        </plugins>
    </build>

我的问题:

使用 Maven,我如何分别运行组一"和组二".

Using Maven, how do I run the group "one" and group "two" separately.

我尝试了mvn test -Dgroups=two",但它只能正常运行(所有测试).

I tried "mvn test -Dgroups=two" but it only runs as normal(all tests).

注意:我使用 2 个不同的配置文件以不同的值运行组一"两次.这就是您在 pom.xml 文件中看到配置文件配置的原因.

Note: I use 2 different profiles to run the group "one" twice with different values. This is the reason you see profile configuration in the pom.xml file.

推荐答案

您可以使用 beanshell 表达式来完成这项工作.

You can make use of a beanshell expression for getting this done.

您首先将如下所示的 beanshell 表达式添加到您的套件 xml 文件中.

You first add a beanshell expression as below to your suite xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
    <test name="Test">
        <method-selectors>
            <method-selector>
                <script language="beanshell">
                <![CDATA[whatGroup = System.getProperty("groupToRun");
                groups.containsKey(whatGroup);
                ]]>
                </script>
            </method-selector>
        </method-selectors>
        <classes>
            <class name="organized.chaos.GroupsPlayGround" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

通过这种方式,您可以使用 IDE 中的套件 xml,并且仍然可以选择要执行的组.如果未通过 JVM 参数 -DgroupToRun=one

This way you can make use of the suite xml from your IDE and still choose which groups to be executed. You can enrich this beanshell to run everything by default if no value is provided via the JVM argument -DgroupToRun=one

有关 beanshell 执行的更多信息,请参阅:

For more information on beanshell execution refer:

  1. 官方 TestNG 文档此处
  2. 我的博文 link

这篇关于从 Maven 运行特定的 TestNG 组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆