提问者:小点点

覆盖BeforeClass方法正在停止整个测试执行


我正在使用TestNG与Selenium WebDriver,我使用的框架有一个基类,它有BeforeClass方法,所有的Suite类都从这个基类扩展,并覆盖了BeforeClass方法,如图所示。

public BaseClass{
    @BeforeClass
    public void preConditions{
        //primary actions like opening browser and setting preferences
    }
}

public TestSuiteClass extends BaseClass{
    @BeforeClass
    @Override
    public void preConditions(){
        super.preCnditions();
        //specific preconditions required by test suite
    }
}

我遇到的问题是,如果一个被覆盖的方法由于任何原因失败,所有的测试服/案例都会被跳过,整个测试执行都会停止。如何阻止这种情况发生?


共2个答案

匿名用户

如果在“之前”中失败了…注释过程中,测试将被跳过而不是失败,因为问题不在于你的测试用例,而在于之前的过程。所以看起来你的车不能在断桥上过河,但这不是你的车的错。

如果你真的想做一些黑客,你可以在这里找到想法!

匿名用户

你可以找到一种方法来解决这个问题,但是你需要考虑你是否真的想这样做。带有@BeforeClass注释的方法的想法是为测试设置相关数据(你甚至称之为前置条件)。如果它失败了,测试无论如何都不会通过。

一种方法是删除@BeforeClass注释并从测试本身调用方法

public TestSuiteClass etends BaseClass {

    public void customPreConditions() {
        super.preCnditions();
    }

    @Test
    publuc void someTest() {
        customPreConditions();
        // do the test
    }
}

即使自定义条件()不成功,测试也会继续。