Depends on Groups
You can also have methods that depend on groups.
Lets create a java class
import org.testng.annotations.Test;
publicclass dependsonfroup {
@Test(groups = {"exculdegroup"} )
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = { "functest" })
publicvoid testMethod2() {
System.out.println("This is testMethod2");
}
@Test(dependsOnGroups = { "functest" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="dependsonfroup"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results:
Here, first it will execute testMethod2 then testMethod1 then tetMethod3, testMethod3 will be executed depends on testMethod2 because testMethod3 execution is depends on testMethod2.
Another example for failed group
Lets create a java class
import org.testng.annotations.Test;
publicclass dependsonfroup {
@Test(groups = {"exculdegroup"} )
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = { "functest" })
publicvoid testMethod2() {
System.out.println("This is testMethod2");
inti=1/0;
}
@Test(dependsOnGroups = { "functest" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="dependsonfroup"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Here, TestMethod2 is failed , TestMethod3 will be skipped.
Alternatively, you can specify your group dependencies in the testng.xml file itself. You use the <dependencies> tag to achieve this:
Lets create a java class
import org.testng.annotations.Test;
publicclass dependsonfroup {
@Test(groups = {"exculdegroup"} )
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = { "functest" })
publicvoid testMethod2() {
System.out.println("This is testMethod2");
}
@Test(groups = { "functest1" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<groups>
<dependencies>
<groupname="functest1"depends-on="functest"/>
</dependencies>
</groups>
<classes>
<classname="dependsonfroup"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
The <depends-on> attribute contains a space-separated list of groups (means you want to list more than one group).
Ex: <groupname="functest1"depends-on="functest exculdegroup"/>
Lets create java class
import org.testng.annotations.Test;
publicclass dependsonfroup {
@Test(groups = {"exculdegroup"} )
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = { "functest" })
publicvoid testMethod2() {
System.out.println("This is testMethod2");
}
@Test(groups = { "functest1" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<groups>
<dependencies>
<groupname="functest1"depends-on="functest exculdegroup"/>
</dependencies>
</groups>
<classes>
<classname="dependsonfroup"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Here, functest1 group is depends on functest and exculdegroup.
Another Example
You can you regular expression in TestNGxml for similar group names.
import org.testng.annotations.Test;
publicclass dependsonfroup {
@Test(groups = {"exculdegroup"} )
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = { "functest1" })
publicvoid testMethod2() {
System.out.println("This is testMethod2");
}
@Test(groups = { "functest2" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}
Same TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<groups>
<dependencies>
<groupname="exculdegroup"depends-on="functest.*"/>
</dependencies>
</groups>
<classes>
<classname="dependsonfroup"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Note:
If a method depended upon fails and you have a hard dependency on it (alwaysRun=false, which is the default), the methods that depend on it are not marked as FAIL but as SKIP. Skipped methods will be reported as such in the final report (in a color that is neither red nor green in HTML), which is important since skipped methods are not necessarily failures.
Both dependsOnGroups and dependsOnMethods accept regular expressions as parameters. For dependsOnMethods, if you are depending on a method which happens to have several overloaded versions, all the overloaded methods will be invoked. If you only want to invoke one of the overloaded methods, you should use dependsOnGroups.
Comments
Post a Comment