gpt4 book ai didi

java - 带有组的 TestNG BeforeMethod

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:16 24 4
gpt4 key购买 nike

我想知道 @BeforeMethod 对组的用法。在 http://testng.org/javadoc/org/testng/annotations/BeforeMethod.html它说:

alwaysRun: If set to true, this configuration method will be run regardless of what groups it belongs to.

所以我有以下类(class):

public class BeforeTest {
private static final Logger LOG = Logger.getLogger(BeforeTest.class);
@BeforeMethod(groups = {"g1"}, alwaysRun = false)
public void setUpG1(){
sleep();
LOG.info("BeforeMethod G1");
}

@Test(groups = {"g1"})
public void g1Test(){
sleep();
LOG.info("g1Test()");
}

@BeforeMethod(groups = {"g2"}, alwaysRun = false)
public void setUpG2(){
sleep();
LOG.info("BeforeMethod G2");
}

@Test(groups = {"g2"})
public void g2Test(){
sleep();
LOG.info("g2Test()");
}


private void sleep(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

哪些输出:

BeforeMethod G1
BeforeMethod G2
g1Test()
BeforeMethod G1
BeforeMethod G2
g2Test()

除了我认为 awaysRun 默认情况下为 false 的事实之外,谁能向我解释为什么在每次测试之前调用两个 before 方法,而忽略组? @Test(skipBeforeMethod = "setUpG1") 之类的东西也可以。

我正在使用 IntelliJ IDEA CE 10.5.2。我也用 gradle 1.0-milestone-3 运行它。

最佳答案

那是因为 groups 标志只表示一个方法是否属于一个组。当启用该组时,它将始终执行,即在所有测试方法之前执行,而不管测试方法属于哪个组。默认情况下,所有组都已启用。

如果您想让该方法只对某些组的测试执行,您需要指定 onlyForGroups

public class BeforeTest {

private static final Logger LOG = Logger.getLogger(BeforeTest.class);

@BeforeMethod(onlyForGroups = { "g1" })
public void setUpG1() {
sleep();
LOG.info("BeforeMethod G1");
}

@Test(groups = { "g1" })
public void g1Test() {
sleep();
LOG.info("g1Test()");
}

@BeforeMethod(onlyForGroups = { "g2" }, alwaysRun = false)
public void setUpG2() {
sleep();
LOG.info("BeforeMethod G2");
}

@Test(groups = { "g2" })
public void g2Test() {
sleep();
LOG.info("g2Test()");
}

private void sleep() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

输出是

BeforeMethod G1
g1Test()
BeforeMethod G2
g2Test()

关于java - 带有组的 TestNG BeforeMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939892/

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