In this example, two TestNG groups were introduced and @BeforeTest & @AfterTest annotations were used to reconfigure the server in each test group execution. Test was design to have single class to include all configuration methods and another test class to hold the tests case. In the test suite XML two test segments were introduced by including groups to run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.testng.annotations.*; | |
import static org.testng.Assert.assertEquals; | |
public class BeforeTestGroups { | |
@Test(groups = {"g1", "g2"}) | |
public void test1(){ | |
sleep(); | |
System.out.println("test1()"); | |
} | |
@Test(groups = {"g1", "g2"}) | |
public void test2(){ | |
sleep(); | |
System.out.println("test2()"); | |
} | |
private void sleep(){ | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException ignored) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.testng.annotations.AfterTest; | |
import org.testng.annotations.BeforeTest; | |
public class MainGroupClass { | |
@BeforeTest(groups = {"g1"}, alwaysRun = false) | |
public void setUpG1() { | |
//configuration goes here | |
sleep(); | |
System.out.println("BeforeMethod G1"); | |
} | |
@AfterTest(groups = {"g1"}, alwaysRun = false) | |
public void tearDownG1() { | |
//revert configuration | |
sleep(); | |
System.out.println("AfterMethod G1"); | |
} | |
@BeforeTest(groups = {"g2"}, alwaysRun = false) | |
public void setUpG2() { | |
//configuration goes here | |
sleep(); | |
System.out.println("BeforeMethod G2"); | |
} | |
@AfterTest(groups = {"g2"}, alwaysRun = false) | |
public void tearDownG2() { | |
//revert Configuration | |
sleep(); | |
System.out.println("BeforeMethod G2"); | |
} | |
private void sleep() { | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException ignored) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > | |
<suite name="Suite1" verbose="1" > | |
<test name="TestG1"> | |
<groups> | |
<run> | |
<include name="g1"/> | |
</run> | |
</groups> | |
<classes> | |
<class name="org.wso2.carbon.tests.BeforeTestGroups"/> | |
<class name="org.wso2.carbon.tests.MainGroupClass"/> | |
</classes> | |
</test> | |
<test name="TestG2"> | |
<groups> | |
<run> | |
<include name="g2"/> | |
</run> | |
</groups> | |
<classes> | |
<class name="org.wso2.carbon.tests.BeforeTestGroups"/> | |
<class name="org.wso2.carbon.tests.MainGroupClass"/> | |
</classes> | |
</test> | |
</suite> |
Execution result of above suite as follows
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
BeforeMethod G1
test1()
test2()
AfterMethod G1
BeforeMethod G2
test1()
test2()
BeforeMethod G2
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.664 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
No comments:
Post a Comment