C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TestNG Annotation AttributesWhile writing the test cases in the TestNG, you need to mention the @Test annotation before the test method. @Test public void testcase1() { System.out.println("This is testcase1"); } In the above code, we have specified the @Test annotation before the test method, i.e., testcase1(). We can also explicitly specify the attributes in a @Test annotation. Test attributes are the test specific, and they are specified at the right next to the @Test annotation. @Test(attribute="value") public void testcase2() { System.out.println("This is testcase2"); } Some of the common attributes are described below:
descriptionIt is a string which is attached to the @Test annotation that describes the information about the test. Let's understand through an example. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Class1 { @Test(description="This is testcase1") public void testcase1() { System.out.println("HR"); } @Test(description="This is testcase2") public void testcase2() { System.out.println("Software Developer"); } @Test(description="This is testcase3") public void testcase3() { System.out.println("QA Analyst"); } } In the above code, we have added the description attribute in every test. The "description" attribute provides information about the test. dependsOnMethodsWhen the second test method wants to be dependent on the first test method, then this could be possible by the use of "dependOnMethods" attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run. Let's understand through an example. First case: When a single value is passed in a parameter. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Class1 { @Test public void WebStudentLogin() { System.out.println("Student login through web"); } @Test public void MobileStudentLogin() { System.out.println("Student login through mobile"); } @Test(dependsOnMethods= {"WebStudentLogin"}) public void APIStudentLogin() { System.out.println("Student login through API"); } } We know that the TestNG executes the test methods in alphabetical order so, in the above program, APIStudentLogin() will execute first. However, we want WebStudentLogin() method to be executed before the execution of the APIStudentLogin() method, so this would only be possible through the "dependsOnMethods" attribute. In the above program, we have specified "dependsOnMethods" attribute in an APIStudentLogin() test method and its value is "WebStudentLogin" which means that WebStudentLogin() method will be executed before the APIStudentLogin() method. Output In the above output, MobileStudentLogin() runs before the WebStudentLogin() method as TestNG runs the test methods in an alphabetical order. Second case: When multiple values are passed in a parameter. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Depends_On_Groups { @Test(dependsOnMethods= {"testcase3","testcase2"}) public void testcase1() { System.out.println("This is test case1"); } @Test public void testcase2() { System.out.println("This is test case2"); } @Test public void testcase3() { System.out.println("This is test case3"); } } In the above code, testcase1() is dependent on two methods, i.e., testcase2() and testcase3(), which means that these two methods will be executed before the testcase1(). Output priorityWhen no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order. Priority determines the sequence of the execution of the test cases. The priority can hold the integer values between -5000 and 5000. When the priority is set, the lowest priority test case will run first and the highest priority test case will be executed last. Suppose we have three test cases and their priority values are -5000, 0, 15, then the order of the execution will be 0,15,5000. If priority is not specified, then the default priority will be 0. Let's understand through an example. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Fruits { @Test public void mango() { System.out.println("I am Mango"); } @Test(priority=2) public void apple() { System.out.println("I am Apple"); } @Test(priority=1) public void watermelon() { System.out.println("I am Watermelon"); } } In the above code, the default priority of mango() test method is 0, so it will be executed first. The watermelon() test method will run after mango() method as the priority of watermelon() test method is 2. The apple() test method has the highest priority, so it will be executed last. Output enabledThe 'enabled' attribute contains the boolean value. By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value. Let's understand through an example. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Programming_languages { @Test public void c_language() { System.out.println("C language"); } @Test(enabled=false) public void jira() { System.out.println("JIRA is a testing tool"); } @Test public void java() { System.out.println("JAVA language"); } } In the above code, the value of the enabled attribute in jira() test method is false, so this method will not be invoked. Output groupsThe 'groups' attribute is used to group the different test cases that belong to the same functionality. Let's understand through an example. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Software_Company { @Test(groups= {"software company"}) public void infosys() { System.out.println("Infosys"); } @Test public void technip() { System.out.println("Technip India Ltd"); } @Test(groups= {"software company"}) public void wipro() { System.out.println("Wipro"); } } testng.xml Output timeOutIf one of the test cases is taking a long time due to which other test cases are failing. To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases. The timeOut is a time period provided to the test case to completely execute its test case. Let's understand through an example. package com.TheDeveloperBlog; import org.testng.annotations.Test; public class Timeout_program { @Test(timeOut=200) public void testcase1() throws InterruptedException { Thread.sleep(500); System.out.println("This is testcase1"); } @Test public void testcaes2() { System.out.println("This is testcase2"); } @Test public void testcase3() { System.out.println("This is testcase3"); } } In the above code, inside the testcase1() method, we have Thread.sleep(500) which means that the testcase1() method will be executed after 500 milliseconds, but we have provided timeOUT attribute with the value 200 means that the testcase1() will be failed after 200 milliseconds. testng.xml Output The above screen shows that one test case is failed and other test cases are passed.
Next TopicTestNG Parameters
|