我在 Eclipse 中使用 Selenium Web 驱动程序和 JUnit。我需要创建简单的检查页面上是否存在某些文本 - 如果存在,那么我需要生成错误。我希望此错误显示在 JUnit 故障跟踪中。这是我的代码:
public class Check {
@Rule
public ErrorCollector errCollector = new ErrorCollector();
@FindBy(tagName="body")
@CacheLookup
private WebElement titl;
@FindBy(partialLinkText="Exception")
@CacheLookup
private WebElement excep;
public void check_false(String t, String r) {
try {
String bodyText = titl.getText();
Assert.assertFalse("Exception found", bodyText.contains(t)); }
catch (AssertionError e) {
System.err.println(r + ": " + e.getMessage());
System.out.println(excep.getText());
errCollector.addError(e);
}
}
如果出现错误,它会显示在 Eclipse 控制台中,但测试是否在 JUnit 中显示为没有错误并且没有显示异常消息。如何正确进行检查?
最佳答案
我用
AssertJUnit.assertFalse("Exception found", bodyText.contains(t));
来自http://testng.org/doc/index.html 请参阅http://testng.org/javadoc/org/testng/AssertJUnit.html
在 Eclipse 中,当我的测试失败时,在 junit 窗口中我会得到 stackstrace。从未尝试过收集错误。它会抛出一个
AssertionFailedError
如果测试失败,但如果你捕获它,我不知道堆栈跟踪是否会出现在 JUnit 窗口中。
关于java - 在 JUnit 故障跟踪中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608808/