gpt4 book ai didi

annotations - 如果在 TestNG 中执行组,则不会调用 @Before Suite 和 @BeforeTest 方法

转载 作者:行者123 更新时间:2023-12-02 07:20:53 25 4
gpt4 key购买 nike

下面是我的 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 过滤条件。

关于annotations - 如果在 TestNG 中执行组,则不会调用 @Before Suite 和 @BeforeTest 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com