TestNG中BeforeClass和BeforeTest方法的区别
一个TestNG类可以包含各种TestNG方法,例如@BeforeTest,@AfterTest,@BeforeSuite,@BeforeClass,@BeforeMethod,@test等。根据执行顺序,@BeforeTest首先执行,然后@BeforeClass执行。但是,如果有多个TestNG类并且每个类中有多个测试,则这些方法的行为就会很明显。
@BeforeTest
此方法在整个执行过程中只执行一次,在调用第一个@Test方法之前。无论存在多少个@Test标签,或者有多少个类包含@Test标签,或者多个类具有多个测试标签,这都不重要。根据testing.xml文件,一旦执行开始,@BeforeTest方法在第一个@Test方法和@BeforeClass以及@BeforeMethod(如果存在)执行之前只执行一次。
@BeforeClass
此方法每个类执行一次,即@BeforeClass的执行次数等于要执行的TestNG类的数量。根据testing.xml,如果需要执行10个TestNG类,则@BeforeClass将在每个类开始运行之前每次执行。每个类中存在多少个@Test标签并不重要。
根据最佳实践,建议为所有通用的@Before方法创建一个单独的类,以便可以根据需要执行这些方法,并避免重复调用。
这些差异中的关键点是
@BeforeTest方法在第一个@Test方法之前只执行一次。
@BeforeClass在每个类之前执行。
如果在不同的类中存在单独的@BeforeTest和@BeforeClass方法,则所有@BeforeTest方法将首先执行,但@BeforeClass方法将根据各自的类执行。
如果所有测试类都扩展了单独类中存在的通用@BeforeTest和@BeforeClass方法,则@BeforeTest将只执行一次,但是相同的@BeforeClass方法将在每个类之前执行。
场景1
假设有两个TestNG类,每个类都有两个@Test标签。每个类都扩展了包含@BeforeTest和@BeforeClass方法的公共类。在这种情况下,@BeforeTest将只执行一次,但是相同的@BeforeClass方法将执行两次,每次在每个类之前。
解决此问题的方法/算法
步骤1 - 创建一个公共的TestNG类beforemethods并编写@BeforeTest和@BeforeClass方法。
步骤2 - 创建两个TestNG类,每个类都扩展公共类(beforemethods) - OrderofTestExecutionInTestNG和NewTestngClass
步骤3 - 在这两个类中的每一个中编写两个不同的@Test方法 - OrderofTestExecutionInTestNG和NewTestngClass。
步骤4 - 现在创建如下所示的testNG.xml来运行这两个TestNG类,而不是公共类。
步骤5 - 最后,运行testNG.xml或在IDE中直接运行TestNG类,或者使用命令行编译并运行它。
示例
对公共TestNG类beforemethods使用以下代码 -
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class beforemethods {
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforeTest");
}
}对TestNG类OrderofTestExecutionInTestNG使用以下代码 -
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG extends beforemethods{
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
}对公共TestNG类NewTestngClass使用以下代码 -
import org.testng.annotations.Test;
public class NewTestngClass extends beforemethods{
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
}testng.xml
这是一个配置文件,用于组织和运行TestNG测试用例。当需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<classes>
<class name = "OrderofTestExecutionInTestNG"/>
<class name = "NewTestngClass"/>
</classes>
</test>
</suite>输出
in beforeTest in beforeClass in test case 1 of OrderofTestExecutionInTestNG in test case 2 of OrderofTestExecutionInTestNG in beforeClass in test case 1 of NewTestngClass in test case 2 of NewTestngClassin afterSuite
场景2
假设有两个TestNG类,每个类都有两个@Test标签和单独的@BeforeTest以及@BeforeClass方法。在这种情况下,所有@BeforeTest方法将首先执行,但@BeforeClass方法将根据其各自的类执行。
解决此问题的方法/算法
步骤1 - 创建两个TestNG类,OrderofTestExecutionInTestNG和NewTestngClass
步骤2 - 在这两个类中的每一个中编写两个不同的@Test方法,OrderofTestExecutionInTestNG和NewTestngClass。
步骤3 - 在这两个类中的每一个中编写不同的@BeforeTest方法,OrderofTestExecutionInTestNG和NewTestngClass。
步骤4 - 在这两个类中的每一个中编写不同的@BeforeClass方法,OrderofTestExecutionInTestNG和NewTestngClass。
步骤5 - 现在创建如下所示的testNG.xml来运行这两个TestNG类。
步骤6 - 现在,运行testNG.xml或在IDE中直接运行TestNG类,或者使用命令行编译并运行它。
示例
对TestNG类OrderofTestExecutionInTestNG使用以下代码 -
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class OrderofTestExecutionInTestNG {
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass of OrderofTestExecutionInTestNG");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforeTest of OrderofTestExecutionInTestNG");
}
}对公共TestNG类NewTestngClass使用以下代码 -
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforTest of NewTestngClass");
}
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass of NewTestngClass");
}
}testng.xml
这是一个配置文件,用于组织和运行TestNG测试用例。当需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<classes>
<class name = "OrderofTestExecutionInTestNG"/>
<class name = "NewTestngClass"/>
</classes>
</test>
</suite>输出
in beforTest of NewTestngClass in beforeTest of OrderofTestExecutionInTestNG in beforeClass of OrderofTestExecutionInTestNG in test case 1 of OrderofTestExecutionInTestNG in test case 2 of OrderofTestExecutionInTestNG in beforeClass of NewTestngClass in test case 1 of NewTestngClass in test case 2 of NewTestngClassin
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP