带组的 TestNG BeforeMethod [英] TestNG BeforeMethod with groups

查看:33
本文介绍了带组的 TestNG BeforeMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am wondering about @BeforeMethod's usage with groups. In http://testng.org/javadoc/org/testng/annotations/BeforeMethod.html it says:

alwaysRun:如果设置为 true,则无论它属于哪个组,都会运行此配置方法.

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

所以我有以下课程:

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") 这样的东西也可以工作.

Aside the fact that I think awaysRun is false by default, can anyone explain to me why both before methods are called before each test, disregarding the groups? Something like @Test(skipBeforeMethod = "setUpG1") would work too.

我使用的是 IntelliJ IDEA CE 10.5.2.我也使用 gradle 1.0-milestone-3 运行它.

I am using IntelliJ IDEA CE 10.5.2. I've run it with gradle 1.0-milestone-3, too.

推荐答案

那是因为 groups 标志只指示一个方法是否属于一个组.当该组被启用时,它总是被执行,即在所有测试方法之前执行,而不管测试方法属于哪个组.默认情况下,所有组都处于启用状态.

That is because the groups flag only indicates if a method belongs to a group. When that group is enabled, it is executed always, i.e. before all tests methods irrespective of the group the test methods belong to. By default all groups are enabled.

如果您希望该方法仅针对某些组的测试执行,则需要指定onlyForGroups.

If you want to have the method only executed for the tests of certain groups, you would need to specify 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()

这篇关于带组的 TestNG BeforeMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆