Note : Here login test execute first . if it fails CreateDashboard and Logout will skip.
(Depended method or group will skip execution.)
DependsOnMethods :
@Test
public
void
Login() {
System.out.println(
"testAdd()"
);
Calculator calculator =
new
Calculator();
Assert.assertEquals(calculator.add(
15
,
2
) ,
17
);
}
@Test
(
dependsOnMethods
={
"Login"})
public
void
CreateDashboard() {
System.out.println(
"testDivide()"
);
Calculator calculator =
new
Calculator();
Assert.assertEquals(calculator.divide(
16
,
0
),
16
);
}
@Test
(
dependsOnMethods
={
"Login"
,
"CreateDashboard"
})
public
void
Logout() {
System.out.println(
"testProcessRealNumbers()"
);
}
DependsOnGroups :
http://websystique.com/java/testing/testng-dependsongroups-example/#:~:text=dependsOnGroups%20%3A%20dependsOnGroups%20attribute%20on%20a,it%20depends%20on%20executed%20successfully.
package com.websystique.testng; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; public class TesNGDependsOnGroupsExample { @BeforeGroups ( "security" ) public void setUpSecurity() { System.out.println( "setUpSecurity()" ); } @AfterGroups ( "security" ) public void tearDownSecurity() { System.out.println( "tearDownSecurity()\n" ); } @BeforeGroups ( "database" ) public void setUpDatabase() { System.out.println( "setUpDatabase()" ); } @AfterGroups ( "database" ) public void tearDownDatabase() { System.out.println( "tearDownDatabase()\n" ); } @BeforeGroups (value= "ui" ) public void setUpUI() { System.out.println( "setUpUI()" ); } @AfterGroups (value= "ui" ) public void tearDownUI() { System.out.println( "tearDownUI()\n" ); } @Test (groups= "database" ) public void testInsert(){ System.out.println( "testInsert()" ); } @Test (groups= "database" ) public void testUpdate(){ System.out.println( "testUpdate()" ); } @Test (groups= "database" ) public void testDelete(){ System.out.println( "testDelete()" ); } @Test (groups= "security" ) public void accessHomePage() { System.out.println( "accessHomePage()" ); } @Test (groups= "security" ) public void accessAdminPage() { System.out.println( "accessAdminPage()" ); throw new RuntimeException(); } @Test (groups= "ui" ) public void openConfirmationDialog() { System.out.println( "openConfirmationDialog()" ); } @Test (groups= "ui" ) public void openFileDialog() { System.out.println( "openFileDialog()" ); } @Test (dependsOnGroups= { "ui" }) public void uiGroupDependentTest(){ System.out.println( "uiGroupDependentTest()" ); } @Test (dependsOnGroups= { "security" , "database" }) public void backendGroupDependentTest1(){ System.out.println( "backendGroupDependentTest1()" ); } @Test (dependsOnGroups= { "security" , "database" }, alwaysRun= true ) public void backendGroupDependentTest2(){ System.out.println( "backendGroupDependentTest2()" ); } } |
Above test class contains three groups security,database & ui. Then we have bunch of tests that belong to different groups. And at last, we have three methods which depends on different groups.
Notice the accessAdminPage test (from security group) which will throw a RuntimeException. That means group ‘security’ will not be successfully executed
Notice the accessAdminPage test (from security group) which will throw a RuntimeException. That means group ‘security’ will not be successfully executed
No comments:
Post a Comment