【问题标题】:Continuing execution even if assert statement fails in selenium using Junit 4 framework [duplicate]即使使用 Junit 4 框架在 selenium 中断言语句失败也继续执行 [重复]
【发布时间】:2016-07-23 11:11:55
【问题描述】:

在我的测试用例中,必须使用多个断言。问题是,如果一个断言失败,那么执行就会停止。我希望测试用例即使在遇到断言失败后也能继续执行,并在执行后显示所有断言失败。

例如:

assertTrue("string on failure",condition1);
assertTrue("string on failure",condition2);
assertTrue("string on failure",condition3);
assertTrue("string on failure",condition4);
assertTrue("string on failure",condition5);

在此示例中,我希望如果 assert 对于 condition2 失败,那么它应该继续执行并在完成执行后显示所有失败。

【问题讨论】:

    标签: unit-testing selenium selenium-webdriver junit ui-testing


    【解决方案1】:

    对于纯粹的 JUnit 解决方案,使用 ErrorCollector TestRule 来处理您的断言。

    ErrorCollector 规则在测试执行完成之前不会报告。

    import org.hamcrest.core.IsEqual;
    import org.hamcrest.core.IsNull;
    import org.hamcrest.text.IsEmptyString;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ErrorCollector;
    
    public class ErrorCollectorTest {
        @Rule
        public ErrorCollector collector = new ErrorCollector();
    
        @Test
        public void testMultiAssertFailure() {
            collector.checkThat(true, IsEqual.equalTo(false));
            collector.checkThat("notEmpty", IsEmptyString.isEmptyString());
            collector.checkThat(new Object(), IsNull.nullValue());
            collector.checkThat(null, IsNull.notNullValue());
    
            try {
                throw new RuntimeException("Exception");
            } catch (Exception ex){
                collector.addError(ex);
            }
        }
    }
    

    在您的具体示例中:

    assertTrue("string on failure",condition1);
    assertTrue("string on failure",condition2);
    assertTrue("string on failure",condition3);
    assertTrue("string on failure",condition4);
    assertTrue("string on failure",condition5);
    

    会变成

    Matcher<Boolean> matchesTrue = IsEqual.equalTo(true);
    collector.checkThat("String on Failure", condition1, matchesTrue);
    collector.checkThat("String on Failure", condition2, matchesTrue);
    collector.checkThat("String on Failure", condition3, matchesTrue);
    collector.checkThat("String on Failure", condition4, matchesTrue);
    collector.checkThat("String on Failure", condition5, matchesTrue);
    

    【讨论】:

      【解决方案2】:

      你要找的功能叫做软断言,试试assertj

          SoftAssertions soft = new SoftAssertions();
          soft.assertThat(<things>).isEqualTo(<other_thing>);
          soft.assertAll();
      

      软断言将允许执行到下一步,而不会在失败时抛出异常。最后assertAll() 方法一次性抛出所有收集的错误。

      【讨论】:

      【解决方案3】:

      这里的另一个选项是一个最佳实践,许多人说无论如何你都应该这样做:在每个测试用例中只放置 一个断言。

      通过这样做,每个潜在的故障都以某种方式相互隔离;你会直接得到你正在寻找的东西——因为 JUnit 会准确地告诉你,哪些测试失败了,哪些通过了。无需介绍其他概念。

      (您会看到,即使 ErrorCollector 或 SoftAssertions 等其他概念使用起来非常简单直接 - 它们会为您的代码增加一点复杂性;使其更难阅读和理解)

      【讨论】:

      • 在 unit 测试中,每个测试最好只做一个断言。随着 Selenium 等工具的加入,测试不再是“单元”级别的测试,因为它必须针对实时系统运行。这是一个“整合”测试。在测试的集成级别,隔离验证的愿望通常是有代价的(时间/性能/反馈),这使得多断言工具更容易被接受。这完全取决于测试所针对的上下文。我不同意你关于单元测试的说法,但在集成时,这条规则并不坚定(根据我的经验)
      • ya.. 我也想过这个.. 但是在许多情况下,比如验证小部分的 UI(例如登录页面),多重断言是有意义的。此外,上述建议的解决方案只增加了 1 或 2 行代码,而不是很复杂
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多