Attribute in @Test annotation
Below are some list of attribute which can use with @Test annotation. These attribute can be used to make @Test annotation more effective and useful.
Enable Attribute-
Enable attribute can have true OR false value, which can enable or disable the test method. If this value is set as true then the method will invoke and when the value is set as false it will skip the method.
In below example, we have kept one method enabled value as true and another method as false. The value set as false will not execute.
Java Code-
public class Testing_Enabled { @Test(enabled=true) public void testNG1() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } @Test(enabled=false) public void testNG2() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } }
Outcome-
[RemoteTestNG] detected TestNG version 7.4.0 Method name is :testNG1 PASSED: testNG1 =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 ===============================================
alwaysRun Attribute-
This is used, when we want to make sure that the method always run even if the the method depend on other method get failed. When the value is set as true, then this test method will always run.
Here in below example, "testNG1 method is depend ofn "testNG3 method."testNG3" will throw exception but still "testNG1" method will get executed
Java Code-
public class Testing_AlwaysRun { @Test(alwaysRun= true,dependsOnMethods="testNG3") public void testNG1() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Always Run Test:-"+methodName); } @Test() public void testNG2() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } @Test() public void testNG3() { int i=2/0; System.out.println(i); } }
Output-
[RemoteTestNG] detected TestNG version 7.4.0 Method name is :testNG2 Always Run Test:-testNG1 PASSED: testNG1 PASSED: testNG2 FAILED: testNG3 java.lang.ArithmeticException: / by zero at org.Testngpro.Testing_AlwaysRun.testNG3(Testing_AlwaysRun.java:24) . . =============================================== Default test Tests run: 3, Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Passes: 2, Failures: 1, Skips: 0 ===============================================
Related topics-
description Attribute-
In description, we can provide the high level description about the methods.
Java Code-
public class Testing_Description { @Test(description="This is TestNG") public void testNG1() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } @Test public void testNG2() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } }
Output-
[RemoteTestNG] detected TestNG version 7.4.0 Method name is :testNG1 Method name is :testNG2 PASSED: testNG1 This is TestNG PASSED: testNG2 =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 ===============================================
priority Attribute-
This command sets the priority of the test method. Lower priorities will be scheduled first. Any method which doesn’t have any priority will execute first.
Java Code-
public class MethodTestng{ @Test(priority=1) public void testNG1() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } @Test() public void testNG2() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } @Test(priority=0) public void testNG3() { String methodName = new Object(){}.getClass().getEnclosingMethod().getName(); System.out.println("Method name is :"+methodName); } }
Ouput-
[RemoteTestNG] detected TestNG version 7.4.0 Method name is :testNG2 Method name is :testNG3 Method name is :testNG1 PASSED: testNG3 PASSED: testNG1 PASSED: testNG2 =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================
Related topics-
timeOut Attribute-
timeOut attribute is nothing but sets the time limit for the execution of the method; if the method execution is not completed within the given time, then the test method will be considered as fail and marks so.
Java Code-
public class Testing_TimeOut {
@Test(timeOut=2000)
public void testNG1() throws InterruptedException
{
Thread.sleep(2001);
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}
Outcome-
[RemoteTestNG] detected TestNG version 7.4.0 FAILED: testNG1 org.testng.internal.thread.ThreadTimeoutException: Method org.Testngpro.Testing_TimeOut.testNG1() didn't finish within the time-out 2000
No comments:
Post a Comment