使用TestNG使每个测试方法在其自己的测试类实例中运行? [英] Making each test method run in its own instance of a test class with TestNG?

查看:36
本文介绍了使用TestNG使每个测试方法在其自己的测试类实例中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我认为以下代码在TestNG中可以正常运行,尽管事实并非如此:

So I thought the following code would run fine in TestNG, although it doesn't:

public class Tests {
    int i = 0;

    @Test
    public void testA() {
        Assert.assertEquals(0, i);
        ++i;
    }

    @Test
    public void testB() {
        Assert.assertEquals(0, i);
        ++i;
    }
}

有没有办法让TestNG为每种测试方法启动一个新的Tests类?

Is there a way to make TestNG fire up a new Tests class for each test method?

推荐答案

常见的解决方案是使用@BeforeMethod方法设置测试状态

The common solution is to use an @BeforeMethod method to setup test state,

@BeforeMethod
public void setup() {
   i = 0; 
}

这篇关于使用TestNG使每个测试方法在其自己的测试类实例中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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