TestNG stands for Test New Generation. It is mainly used by developers for Unit level testing. So it is a unit level testing framework. Developers use either Junit or TestNG for Unit testing. TestNG is more powerful than Junit. Actually TestNG is advanced or modified version of Junit. Automation Tester integrate this with eclipse to write and execute test test cases. TestNG is open source. Available in the form of jar files. Junit and testng are applicable only with Java , ie, both are java testing frameworks. TestNG generates its own html report.
TestNG is a framework . It has prebuilt jars/libraries ,
annotations, other features like priorities (for sequence of execution),
dependency features, grouping features, data provider feature , to make your job easy.
TESTNG is also called TDD framework, test driven
development.
We add TestNG from Eclipse TestNG plug-in, steps are as below:
Go to Help section in Eclipse
Click Eclipse Marketplace
Click Search tab , type TestNG and hit ENTER
Click install and restart Eclipse
In WebDriver JVM runs the script , in TestNG framework TestNG will run the script.
Add TestNG library(jars).
Annotations are nothing other than classes in TestNG
When you run TestNG , one xml is created by default(testng-results.xml). You can open and see the xml.
In the xml, if you donot assign any suite name, then TestNG gives dafault name as "Default suite", if Test name is not given then it is "Default test".
In console, passed Test cases are displayed by green icon, failed by red icon and skipped by yellow icon.
If more than one @Test are present and you run without priority, then they run in alphabetical order of the method name @Test.
After every annotation there should be a method.
Go to Help section in Eclipse
Click Eclipse Marketplace
Click Search tab , type TestNG and hit ENTER
Click install and restart Eclipse
In WebDriver JVM runs the script , in TestNG framework TestNG will run the script.
Add TestNG library(jars).
Annotations are nothing other than classes in TestNG
When you run TestNG , one xml is created by default(testng-results.xml). You can open and see the xml.
In the xml, if you donot assign any suite name, then TestNG gives dafault name as "Default suite", if Test name is not given then it is "Default test".
In console, passed Test cases are displayed by green icon, failed by red icon and skipped by yellow icon.
If more than one @Test are present and you run without priority, then they run in alphabetical order of the method name @Test.
After every annotation there should be a method.
The sequences of annotations in the TestNG is :
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite – This will be displayed in the test report
separately.
Even if you donot put the annotations in sequence, while execution the TestNG automatically execute them in the sequence.
Even if you donot put the annotations in sequence, while execution the TestNG automatically execute them in the sequence.
You do not need to add all these annotations in your test. As
per your need you can use the annotations.
More than one
TESTs
Suppose you have two Tests. Then , it will execute every
annotations before @beforeMethod , then Test1 and then @AfterMethod, then again
@beforeMethod , then Test2 and @AfterMethod and then all the remaining @after
annotations.
If there are more Tests, the beforemethod , Test and after
method are repeated for each test . all other before and after annotations are
executed once only.
Priority in TESTNG
Suppose you have three Tests , then the before method , test
and after method will be repeated 3 times, but the sequence of tests will be
based on the alphabetical order of method names in the TEST. If you want to run in specific order then you can specify the priority
in each test , like @Test(priority = 1) , @Test(priority =2) , @Test(priority
=3) and so on.
If you give the priorities, with negative value also, like : @Test(priority = 1) , @Test(priority =2) , @Test(priority = -3) , then the priority = -3 with execute first then priority =1 then priority=2 and so on.
If you give the priorities, with negative value also, like : @Test(priority = 1) , @Test(priority =2) , @Test(priority = -3) , then the priority = -3 with execute first then priority =1 then priority=2 and so on.
Report(index.html)
Three kinds of TestNG reports:
Once you execute TESTNG , you get report generated by
TESTNG. You also get a new folder named "test-output" folder created(you might need to refresh
your testing project to see the folder.). Inside the folder, there is another folder "old" in which there is a file
index.html which is output / html report. In which you can see detail
information about your execution, passed/failed results of each tests. This is default html report by testing , ie,
basic report.
There is emailable-report.html also created. You can open this with browser or share the html . It is also a reprot.
There is html report created inside the suite folder also. The suite folder is inside the test-output folder.
Groups in TESTNG(In progress)
You can add group to each test based on whatever you
like/need. For that @Test(priority=2,group=”Benefit1”) . if you want to
associate the test with more than one group you can add more groups as well
such as @Test(priority=2,groups={”Benefit1”, “Category1”}). In the index.html report , you can see the result/report as
per the group also.
DependsOnMethods
Suppose we have two @Test
Then we can write,
@Test(dependsOnMethods=methodNameofAnother @Test)
@Test(dependsOnMethods="methodName") //No brackets "( )" in methodName.
if the first/primary test fails, it does not go to the dependent test, ie, skipped, skipped tests are displayed by yellow color icon. Passed icons green and failed red.
Add Description with @Test
You can add description also with @Test and this description will appear with the html report and console report.
@Test(priority=1, description="This test case will check Registration.")
InvocationCount
Suppose you want to run a particular Test 10 times. Then either you write the @Test 10 times or you can write below code with InvocationCount:
@Test(invocationCount=10)
method
InvocationTimeOut
This is the maximum time given to a @Test to complete execution . Suppose for some reason or infinite loop , if it is taking long time to execute , then it times out at the given time limit.
@Test(invocationTimeOut=5000) // time is in milli seconds , here it is 5 secs.
method
ExpectedExceptions
When this key word is used, we are telling Testng that do not fail or terminate the test execution even when the given exception occurs.
@Test(invocationTimeOut=5000, expectedExceptions = ArithmeticException.class)
public void methodA() {
System.out.println("ContinueExecution");
int i = 1/0; //it will fail at this point and throws Arithmetic Exception
}
In this situation, the execution does not fail even though , it gets arithmetic Exception. So this key word is used if you are expecting certain exception and you wand to ignore/skip that exception.
Assert
To validate if the given condition is pass or fail. This is equivalent to if - else condition in Selenium webDriver. If given condition is satisfied , it is passed otherwise failed and gives assertion error with the print statement.
Assert.assertEquals(actualValue, expectedValue , print statement if fails);
There are more methods available for Assert.
Such as ,
Assert.assertTrue(Condition - true or false)--- pass if true and vice versa.
Assert.assertFalse(Condition - true or false)--- pass if false and vice versa.
Assert.assertEquals(actualValue, expectedValue);
The difference between Assert and Validation Point is :
1. Assert is available in Junit and TestNG only not in Java , In Java we use ValidationPoint or Verify.
2. There are Assert and SoftAssert. SoftAssert is similar to Verify.
3. When Assert condition is not satisfied, it fails/stops/aborts the execution right away , right there. But when Verify or softAssert fails, it does not stops the execution. The execution continues and the end result will be, it is marked or displayed as fail.
DependsOnMethods
Suppose we have two @Test
Then we can write,
@Test(dependsOnMethods=methodNameofAnother @Test)
@Test(dependsOnMethods="methodName") //No brackets "( )" in methodName.
if the first/primary test fails, it does not go to the dependent test, ie, skipped, skipped tests are displayed by yellow color icon. Passed icons green and failed red.
Add Description with @Test
You can add description also with @Test and this description will appear with the html report and console report.
@Test(priority=1, description="This test case will check Registration.")
InvocationCount
Suppose you want to run a particular Test 10 times. Then either you write the @Test 10 times or you can write below code with InvocationCount:
@Test(invocationCount=10)
method
InvocationTimeOut
This is the maximum time given to a @Test to complete execution . Suppose for some reason or infinite loop , if it is taking long time to execute , then it times out at the given time limit.
@Test(invocationTimeOut=5000) // time is in milli seconds , here it is 5 secs.
method
ExpectedExceptions
When this key word is used, we are telling Testng that do not fail or terminate the test execution even when the given exception occurs.
@Test(invocationTimeOut=5000, expectedExceptions = ArithmeticException.class)
public void methodA() {
System.out.println("ContinueExecution");
int i = 1/0; //it will fail at this point and throws Arithmetic Exception
}
In this situation, the execution does not fail even though , it gets arithmetic Exception. So this key word is used if you are expecting certain exception and you wand to ignore/skip that exception.
Assert
To validate if the given condition is pass or fail. This is equivalent to if - else condition in Selenium webDriver. If given condition is satisfied , it is passed otherwise failed and gives assertion error with the print statement.
Assert.assertEquals(actualValue, expectedValue , print statement if fails);
There are more methods available for Assert.
Such as ,
Assert.assertTrue(Condition - true or false)--- pass if true and vice versa.
Assert.assertFalse(Condition - true or false)--- pass if false and vice versa.
Assert.assertEquals(actualValue, expectedValue);
The difference between Assert and Validation Point is :
1. Assert is available in Junit and TestNG only not in Java , In Java we use ValidationPoint or Verify.
2. There are Assert and SoftAssert. SoftAssert is similar to Verify.
3. When Assert condition is not satisfied, it fails/stops/aborts the execution right away , right there. But when Verify or softAssert fails, it does not stops the execution. The execution continues and the end result will be, it is marked or displayed as fail.
No comments:
Post a Comment