TestNG - Attributes
~30 mins
TestNG Attributes
What are Attributes?
- Attributes are meta properties which are used to provide information to the testmethods
Why do we need to use it?
- To ease the execution by injecting dependencies,prioritizing, grouping the testcases.
When to use attributes?
- To control the execution flow.
TestNg –Helper attributes
Available attributes in TestNG:
- priority
- enabled
- invocationCount
- threadpoolsize
- invocationTimeOut
- dependsOnMethod
- alwaysRun
- description
- timeOut
Attributes- Priority
- to prioritize the test method
- works only with @Test method
- executes the test method with lowest priority
- default value=0
Syntax :
@Test(priority=1)
public void m1() {
}
Attribute-enabled
- to ignore the test method from execution
- works only with @Test method
- default value=true
- when set to false, the test method will not be executed
- Syntax :
@Test(enabled=false)
public void m1(){
}
Attribute- invocationCount
- to execute the test method multiple time with same set of data
- works only with @Test method
- default value=1
- Syntax :
@Test(invocationCount=3)
public void m1(){
}
- Method will be executed thrice
- InvocationTimout → time limit will be set to invoke number of invocations
@Test(invocationCount=3,invocationTimeOut=3000)
public void m1(){
}
- Method will be executed thrice for the time unit of 3ms
Attribute -threadPoolSize
- Comes along with invocationCount
- control the number of threads required to execute the test method
- Supports parallel execution at @Test level
- default value=0
- Syntax :
@Test(invocationCount=3,threadPoolSize=2)
public void m1(){
}
*Method will be executed thrice in parallel run with 2 threads
Attribute-dependsOnMethods
-
to inject dependency between two @test methods
-
if the individual method fails, then the dependent method will be skipped from execution
-
Syntax : (within the class)
@Test(dependsOnMethods={"method2","method3"})
public void m1(){
}
@Test
public void method2(){
}
@Test
public void method3(){
}
- Syntax : (between the class)
@Test(dependsOnMethods="packageName.className.methodName")
public void m1(){
}
Attribute -alwaysRun
- executes the dependent test method even though the independent method fails
- default value -false
- Syntax :
@Test(dependsOnMethods="method2" , alwaysRun=true)
public void m1(){
}
@Test
public void method2(){
throw new Exception();
}