【问题标题】:testng - Invocationcount - stop test on first failuretestng - Invocationcount - 在第一次失败时停止测试
【发布时间】:2014-10-07 15:37:31
【问题描述】:

我有一个测试,invocationcount 设置为 20,如果迭代 4 或 5 失败,我希望测试停止。

有什么办法吗?我只是在谷歌上搜索了相同的内容,但找不到任何内容

【问题讨论】:

    标签: testng


    【解决方案1】:

    您可以使用IInvokedMethodListener

    覆盖接口的两种方法。在 afterInvocation 中,检查结果并可能添加到 Map<method, failureCount> 的映射中

    在 beforeInvocation 中,检查 failureCount > 4 然后抛出 SkipException,将导致其余调用被跳过。

    类似:

    static Map<String, Integer> methodFailCount = new HashMap<String, Integer>();
        public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
    
            if(methodFailCount.get(method.getTestMethod().getMethodName())!=  null && methodFailCount.get(method.getTestMethod().getMethodName()) > 4)
                throw new SkipException("Skipped due to failure count > 4");
        }
    
        public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
            if(testResult.getStatus() == TestResult.FAILURE){
                if(methodFailCount.get(method.getTestMethod().getMethodName() ) == null)
                    methodFailCount.put(method.getTestMethod().getMethodName(),1);
                else{
                    methodFailCount.put(method.getTestMethod().getMethodName(),
                            methodFailCount.get(method.getTestMethod().getMethodName() )+1);
                }
    
            }   
    
        }
    

    【讨论】:

    • 太棒了!这很有帮助
    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 2011-12-09
    • 2021-03-19
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2018-12-17
    相关资源
    最近更新 更多