C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TestNG @BeforeClass Annotation@BeforeClass: The @BeforeClass annotated method runs before the execution of test methods in a current class. Let's understand the concept of @BeforeClass annotation through an example: Step 1: Open the Eclipse. Step 2: We create a simple java project. Class1.java
package com.TheDeveloperBlog;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Class1
{
@BeforeClass
public void before_class()
{
System.out.println("This method is executed before Class1");
}
@Test
public void testcase2()
{
System.out.println("Test case2");
}
@Test
public void testcase1()
{
System.out.println("Test case1");
}
}
Class2.java
package com.TheDeveloperBlog;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Class2
{
@BeforeClass
public void before_class()
{
System.out.println("This method is executed before Class2");
}
@Test
public void testcase3()
{
System.out.println("Test case3");
}
@Test
public void testcase4()
{
System.out.println("Test case4");
}
}
We use the @BeforeClass annotated method in the above two classes. Class1 contains the @BeforeClass annotated method, i.e., before_class() which will be invoked before the execution of Class1 and Class2 also contains the @BeforeClass annotated method, i.e., before_class() which will be invoked before the execution of Class2. Step 3: Now, we create the testng.xml file to configure the above two classes, i.e., Class1 and Class2. Step 4: Run the testng.xml file. Right click on the testng.xml file, move the cursor down to Run As and then click on the 1 TestNG Suite. Output
Next TopicTestNG Annotations
|