Running Specific TestNG Groups from Maven

陌路散爱 提交于 2021-02-10 05:17:01

问题


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
}

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>

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>

My Question:

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

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

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.


回答1:


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

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

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

For more information on beanshell execution refer:

  1. Official TestNG documentation here
  2. My blog post link


来源:https://stackoverflow.com/questions/48404514/running-specific-testng-groups-from-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!