【问题标题】:Display errors in JUnit failure trace在 JUnit 故障跟踪中显示错误
【发布时间】:2012-07-23 08:22:21
【问题描述】:

我在带有 JUnit 的 Eclipse 中使用 Selenium Web 驱动程序。我需要创建简单的检查页面上是否存在某些文本 - 如果是,则我需要生成错误。我希望在 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 中显示为没有错误并且没有显示异常消息。如何正确检查?

【问题讨论】:

    标签: java selenium junit webdriver


    【解决方案1】:

    我用

    AssertJUnit.assertFalse("发现异常", bodyText.contains(t));

    来自http://testng.org/doc/index.htmlhttp://testng.org/javadoc/org/testng/AssertJUnit.html

    当我的测试失败时,在 eclipse 中,在 junit 窗口中,我得到了 stackstrace。从未尝试收集错误。它会抛出一个

    AssertionFailedError

    如果测试失败但如果你发现它,我不知道堆栈跟踪是否会在 JUnit 窗口中。

    【讨论】:

    【解决方案2】:

    这可能不是你想要的,在这种情况下忽略它,但你可以简单地fail it 并提供一条可选消息来说明原因。

    public void checkText(String actual, String expected){
      try{
        if(!expected.equals(actuals){
          fail("Expected : [ " expected + " ] , actual [" + actual + "]"
      }catch(SomeOtherException soe){
          fail(soe.getMessage());
      }
     }
    

    【讨论】:

    • 我用这个,但这不是我真正的意思和需要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2010-09-18
    • 2018-10-02
    • 2012-03-11
    相关资源
    最近更新 更多