下面是我的 XML 文件和演示类。 Precondition() 方法将在 demo1() 方法之前运行,postCondition() 方法将在 demo1() 方法之后运行。 demo2() 的过程相同。但是当我运行代码时,不会调用 BeforeSuite 和 BeforeTest 方法。为什么。?怎么称呼他们?
Output :
Before Method is executing
DEMO -1
After Method is executing
Before Method is executing
DEMO 2
After Method is executing
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name = "Hey"></include>
</run>
</groups>
<classes>
<class name="practicepackage.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
package practicepackage;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Demo {
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method is being launched");
}
@BeforeTest
public void beforeTest(){
System.out.println("Before Test Method is being luanched");
}
@BeforeMethod(groups = {"Hey"})
public void PreCondition(){
System.out.println("Before Method is executing");
}
@Test (groups = {"Hey"})
public void demo1(){
System.out.println("DEMO -1 ");
}
@Test(groups = {"Hey"})
public void demo2(){
System.out.println("DEMO 2");
}
@AfterMethod(groups = {"Hey"})
public void postCondition(){
System.out.println("After Method is executing");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name = "Hey"></include>
</run>
</groups>
<classes>
<class name="practicepackage.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
为了确保
@BeforeSuite
和@BeforeTest
始终执行,请为这些注释启用属性alwaysRun=true
。
这是必需的,因为当您运行组时,TestNG 不会选择这些配置方法,除非它们是您选择的组的一部分。
TestNG 中的组选择可以可视化为 TestNG 中的一种过滤机制,当 TestNG 决定运行哪些测试时,可以让您告诉 TestNG 过滤的标准。
像这样使用:
` @BeforeTest(alwaysRun=true)
public void beforeTest() {
System.out.println("BeforeTest FirstTest Class");
}`