TestNG description
In framework, with growing number of automated test cases, it’s hard to have the test method names more descriptive, so testNG provides an attribute description to specify long description to understand what the test method is all about.
@Test(description="This is testcase1 desc") public void test1(){ //to-do } @Test(description="This is testcase2 desc") public void test2(){ //to-do }
TestNG groups
In each release test cases keep adding to existing suite and after many releases it’s difficult to run all test cases, so it’s better to cherry pick couple of test cases from each release and keep them for regression suite.
it’s better to mark the test cases as “regression” and run them at a time instead of running all the test cases.
Same way we might have more modules in our application, and sometimes we need to only run specific module related tests instead of all, in that case testNG helps us to categorise the testNG methods and also provides a way to run one or more or specific groups.
Syntax –
@Test(description="This is testcase1 desc", groups={"funct", "sanity"}) public void test1() { System.out.println("This is method: test1"); }
Now let’s create couple of testNG classes with more testng methods specifying the groups –
TestNGAnnGroups1.java
package testng1; import org.testng.annotations.Test; public class TestNGAnnGroups1 { @Test(description="This is testcase1 desc", groups={"functional", "sanity"}) public void test1() { System.out.println("This is method: test1"); } @Test(description="This is testcase2 desc", groups={"regression"}) public void test2() { System.out.println("This is method: test2"); } }
TestNGAnnGroups2.java
package testng1; import org.testng.annotations.Test; public class TestNGAnnGroups2 { @Test(description="This is testcase12 desc", groups={"sanity"}) public void test12() { System.out.println("This is method: test12"); } @Test(description="This is testcase22 desc", groups={"api", "functional"}) public void test22() { System.out.println("This is method: test22"); } }
Let’s see how we can one or more or specific group of tests –
Using testNG.xml
We can use groups element in testng.xml and can include or exclude the group names to run specific group of tests.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite" parallel="none"> <test name="Login"> <groups> <run> <include name="functional"/> <!--exclude name="regression"/--> <include name="api"/> </run> </groups> <classes> <class name="testng1.TestNGAnnGroups1"/> <class name="testng1.TestNGAnnGroups2"/> </classes> </test> </suite>
When you run above testng.xml file, you will see the output from these test methods with group functional and api
test1()
test22()
to exclude specific groups, we can use exclude element inside the groups > run of testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite" parallel="none"> <test name="Login"> <groups> <run> <exclude name="functional"/> <!--exclude name="api"/--> </run> </groups> <classes> <class name="testng1.TestNGAnnGroups1"/> <class name="testng1.TestNGAnnGroups2"/> </classes> </test> </suite>
When you run above testng.xml file, you will see the output from these test methods will not run
test1()
test22()
Though test22() has both functional and api groups, it won’t run as we have excluded the functional.
Note –
We can mention include and exclude elements / tags in testng.xml, have different combinations of include and exclude groups to understand more about running group tests.
Using command line
To run the tests from command line, make sure you should have sure-fire plugin in pom.xml
Let’s say we want to run functional and sanity group of tests –
mvn test -Dgroups=functional, sanity
to exclude certain groups –
mvn test -DexcludedGroups=api,regression
Using pom.xml sure-fire plugin
You have use the groups element or tag in sure-fire plugin under configuration
plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <groups>functional,api</groups> </configuration> </plugin> [...] </plugins>
The same way if you want to exclude certain groups
<configuration> <excludedGroups>api</excludedGroups> </configuration>
References –
https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
1 Comment