TestNG Notes and interview Questions
Priority:
@ Test(priority=0,1,2,3,4,-1,3,-4)
like that we can set the priority,as a default priority will be 0.and -1 means it execute before 0.and if we have duplicate priority execution will be based on alphapetical order
Similarly,one method has priority -1 but this is depends another method that has priority 10,since this methods depends on second method which has priority 10,this only first exceute even though it has priority 10
Skip:
if we want to skip particular test we can use enabled=false
as a default it will be enabled=true,if we want to skip it should be false
@ Test(enabled=false)
Dependencies:
1.we can set one test method as depends on another method or can set depends on group
2.for this,dependsOnMethods=”methodname” or dependsOnGroups={“smoke”,”regression”}
if depended method fails,this method wont get executed
similarly,if depended groups has atleast one failure,this depended method wont get executed.
only if all methods in that group is passed only this will get executed
Annotation:
BeforeSuite--runs once before all tests,classes,methods
Aftersuite-->runs once after all tests,classes,methods ran successfully
if we give always run for this two annotations ,it work even though other tests failed
TestSuite:
<xml>
<Suite parallel=”methods,classes,tests” thread-count=”10”>
<Listeners><listener class-name=”package.class”></listener></listeners>
<Test>
<Groups><run><include name=”smoke”></include></run>
<exclude name=”regression”></exclude>
</Groups>
<classes>
<Class>
<include
</class></classes>
</test>
</suite>
Listeners:
1.onteststart(): even though test is skipped,this will run
2.ontestsuccess():only run after if test success
3.onTestFailure():only run after if test failed
4.onTestSkipped:only run after test is skipped
5.onTestFailedButWithinPassPercentage:only run if passpercentage
6.onTestFailedwithTimeout
7.onStart:run before all test
8.onFinish:after all complete
Groups:
1.<groups><run><include name=”groupName”></include></run></groups>
if we want to run some set of test cases ,we can group them in a common group and include under groups in testng file.
if we want to exclude some set of testcases,same like before group them in a common group and exclude under groups in testng file…those set of testcases only excluded
Parameterisation:
@ Parameters(value={“sangar”,”sangar2”}
public void sangar(String input1,String input2){}
<Suite><Parameter name=”Sangar” value=”sangar”></Parameter>
Parallel Testing:
<suite name=”sangar” parallel=”tests,methods,classes” thread-count=”10”>
How to Rerun
1.using IRetryAnalyser
class RetryMech implements IRetryAnalyser{
int count=4;
int failed=0;
@override public boolean retry(ITestResult result){
while(failed<count){
failed++;
return true;
}return false}}
@Test (retryAnalyzer=RetryMEch.class)
2.Using IAnnotationTransformer:
class Transformer implements IAnnotationTransformer{
@override public void transform(ITestAnnotation annotation,Test emkmmf){
annotation.setRetryAnalyser(RetryMech.class);
3.run testng-failed.xml
Execute particular Test multiple time:
using invocationcount=5
@ Test(invocationCount=5)
Timeout:
@ Test (timeout=5000)
incase inside script takes more than 5000,it retrun threadtimeoutexception
Assertions:
Assert.assertAll()---->reports all failures
Data Provider:
@ DataProvider (name=”sangar”)
public Object[][] sendData(){
return new Object[][]{{“first”},{“second”}};
}
@ Test(dataProvider=”sangar”)
public void test(String data){
}
@Test( SingleThreaded=true)---->ensures that all methods in that class accessed by same thread.
@ Factory Annotation:
used to create Test methods dynamically at run time.
private string username;
private String password;
constructor(String username,String password){
this.username=username;
this.password=password;}
@ Factory
public Object[] creatingInstance(){
return new Object[]{new Constructor(“Sangar”,”sangar”),new Constructor(“Kunar”,”kunar”)};}
combo @ factory and data provider:
@Data provider(name=”sangar”)
public Object[] giving(){
return new Object[] {1,2,3,4,5,5}};
@Factory (dataProvider=”sangar”)
public Object[] createinInstance(String data){
return new Object[] {
new Constructor(“sangar”+data,”sanga”+data);
new constructopr(pp,)
Comments
Post a Comment