本文介绍了除了 PASS/FAIL,TestNG 是否支持 WARN 类型的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 TestNG 与 Selenium 结合使用来测试具有多页面流程的 Web 应用程序(例如,在 8 个不同页面上注册并完成您的个人资料).

I am using TestNG in conjunction with Selenium to test a web application, which has multi-page flows (e.g. sign up, and complete your profile across 8 different pages).

我使用了 Selenium 的 PageObject 方法并在每个页面中实现了检查代码,例如检查php 错误"消息不会显示在页面中,在这种情况下,如果单个页面检测到此错误,则整个流程(这是一个在内部引用多个 PageObjects 的 @Test 方法)将失败.

I've used Selenium's PageObject's approach and implemented checking code in every page that checks for e.g. "php error" messages are not shown in the page, in this case if a single page detects this error the complete flow (which is a @Test method referencing multiple PageObjects internally) will fail.

我想报告一些错误,但不将整个流程标记为失败(例如,错误地转义引号或 HTML 字符).我可能在所有页面中都有一个常见错误,这并不妨碍整个流程的执行,如果我可以报告警告并且仍然能够继续测试,这将节省时间.

There are some errors that I'd like to report but not mark the whole flow as failed (e.g. incorrectly escaping quotes or HTML characters). I may have a common error in all pages which does not preclude the whole flow from executing and it would save time if I can report the warning and still be able to continue testing.

Reporter 是最好的方法吗?从可用性的角度来看,最好用红色(失败)、绿色(通过)和橙色(警告)颜色显示报告.

Is Reporter the best way to do this? From a usability perspective it would be nice to show reports with RED (fail), GREEN (pass) and ORANGE (warn) colors.

推荐答案

我想出了一种方法来做到这一点,尽管它有点小技巧.在要警告而不是通过失败的测试方法中,执行以下操作:

I figured out a way to do this, though it's a bit of a hack. In the test method where you want to warn instead of pass fail, do this:

...
import org.testng.Reporter;
...

@Test
public void myTestMethod(){
   if( someConditionThatCausesWarning ) {
       Reporter.getCurrentTestResult().setAttribute("warn", "My warning message");
   }
}

这会在测试结果对象上设置一个属性,然后您可以在自定义侦听器和报告器中访问该属性.我将详细程度设置为 0:

This sets an attribute on the test result object, which you can then access in your custom listener and reporter. I set the verbosity to 0:

...
TestNG tng = new TestNG();
tng.setVerbose(0);
...

关闭默认的实时报告.

然后在侦听器和/或报告器中:

Then in the listener and/or reporter you do:

testResult.getAttribute("warn");

查看是否有警告而不是 PASS.

to see if there's a warning instead of a PASS.

这篇关于除了 PASS/FAIL,TestNG 是否支持 WARN 类型的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:02