繁体   English   中英

如果50%的测试方法在testng中失败,如何停止自动化?

[英]How do I stop automation if 50% test methods are failed in testng?

如果编写的@Test方法失败超过50%,我想停止执行。

例如:

public void LoginTest(){

    @Test
    public void ValidUserName(){

    }

    @Test
    public void InValidUserName(){
    }

    @Test
    public void ValidUserID(){
    }

    @Test
    public void ValidUserIDInvalidPassword(){
    }

    @Test
    public void EmptyUserNamePassword(){
    }
}

如果ValidUserName()InValidUserName()ValidUserID()失败,则意味着LoginTest 50%@Test方法失败了,现在,我不想执行ValidUserIDInvalidPassword()EmptyUserNamePassword()

如果有人可以在这方面帮助我,那将是很棒的。

实现IInvokedMethodListener接口,并在达到阈值时引发SkipException 在下面的代码中使用了30%。

public class MyMethodInvoke implements IInvokedMethodListener {

    private int failure = 0;

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        int testCount = testResult.getTestContext().getAllTestMethods().length; 
        if((failure * 1.0) / testCount > 0.3)
            throw new SkipException("Crossed the failure rate");
    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {        
        if(testResult.getStatus()==ITestResult.FAILURE)
            failure++;
    }

}

@Listeners({package.MyMethodInvoke.class})
public class Test {

它适用于单个类中的测试,不知道它如何与套件中多个类中的测试一起表现。 甚至可以并行执行。

您想要的没有任何意义。 尝试使用依赖项重组方法。 来自testNG页面的示例:

@Test
public void serverStartedOk() {}

@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}

参见: http : //testng.org/doc/documentation-main.html#dependent-methods

更新:

这里有successPercentage的概念,但通常按方法使用,并与invocationCount结合使用。 例如,在异步调用中,不能保证100%的调用成功。 因此,可以做到:

//method is invocated 2 times. If 1 passes, test is considered OK/green.
@Test(timeOut = 2000, invocationCount = 2, successPercentage = 50)
public void waitForAnswer() throws InterruptedException{
...}

但这与您想要的不兼容。

更新2: “您想要的没有意义。” ->读取:“ TestNG不支持开箱即用”。 但是有一些解决方法。 请参见http://testng.1065351.n5.nabble.com/how-to-stop-a-test-suite-if-one-method-fails-td13441.html中的漂亮答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM