【问题标题】:SoftAssertion specific to test in TestNG特定于在 TestNG 中测试的 SoftAssertion
【发布时间】:2018-09-13 15:12:33
【问题描述】:

我正在使用 TestNG 的软断言,如以下代码。

public class SoftAssertionTest {
    SoftAssert softAssert = new SoftAssert();

    @Test
    public void test1(){
        softAssert.assertEquals(2, 3);
        softAssert.assertEquals(2, 2);
        softAssert.assertEquals(2, 5);
        softAssert.assertAll();
    }

    @Test
    public void test2(){
        softAssert.assertEquals(2, 2);
        softAssert.assertTrue(false);
        softAssert.assertAll();
    }
}

但是当这个测试类运行时,对于test1,它会返回类似-

的结果
java.lang.AssertionError: The following asserts failed:
    expected [3] but found [2],
    expected [5] but found [2]

这是正确的,但对于test2,它返回test1test2 的失败

java.lang.AssertionError: The following asserts failed:
    expected [3] but found [2],
    expected [5] but found [2],
    expected [true] but found [false]

实际上,它应该只返回test2 的结果。

这可以通过在每个测试方法中创建局部变量SoftAssert 来实现。但这可能是代码的重复。

有什么方法可以使用相同的对象,并且在每个测试方法中清除该对象的测试失败结果,以便在新的测试方法中,它只会捕获与该方法相关的失败。

【问题讨论】:

    标签: selenium selenium-webdriver automation automated-tests testng


    【解决方案1】:

    SoftAssert 类的源代码中只有一个公共方法 - assertAll()。 因此,为每个测试创建一个新的 SoftAssert 实例是一种常见的做法。

    【讨论】:

      【解决方案2】:

      您需要为每个测试用例创建 SoftAssert 对象,而不是使用类变量。所以你的代码应该如下所示:

      public class SoftAssertionTest {
      
      
          @Test
          public void test1(){
              SoftAssert softAssert = new SoftAssert();
              softAssert.assertEquals(2, 3);
              softAssert.assertEquals(2, 2);
              softAssert.assertEquals(2, 5);
              softAssert.assertAll();
          }
      
          @Test
          public void test2(){
              SoftAssert softAssert = new SoftAssert();
              softAssert.assertEquals(2, 2);
              softAssert.assertTrue(false);
              softAssert.assertAll();
          }
      }
      

      如果您在 TestNG 中使用 selenium,您应该使用 TesNG's qaf extension,它为 assertion and verification 提供内置支持,报告 reports 中的每个断言/验证是否通过或失败。

      使用 QAF,您的示例可能如下所示:

      import static com.qmetry.qaf.automation.util.Validator.verifyThat;
      import static com.qmetry.qaf.automation.util.Validator.verifyTrue;
      import static org.hamcrest.Matchers.equalTo;
      
      public class SoftAssertionTest {
      
          @Test
          public void test1(){
             verifyThat(2, equalTo(3));
             verifyThat(2, equalTo(2));
             verifyThat("just for fun",2, equalTo(5));
          }
      
          @Test
          public void test2(){
              verifyThat(2, equalTo(2));
              verifyTrue(false,"failure message","success message");
          }
      }
      

      你会得到如下报告:

      test1
      Check Points:   
      
      
      Expected: <3>
           Actual: was <2>
      
      
      Expected: <2>
           Actual: was <2>
      
      just for fun
      Expected: <5>
           Actual: was <2>
      
      
      test2
      Check Points:   
      
      just for fun
      Expected: <2>
           Actual: was <2>
      
      failure message
      

      您将在每个检查点的 html 报告中看到红色和绿色标记。 请注意,它还为您提供了成功的详细信息。如果是浏览器/移动端,qaf 将附上每个检查点的屏幕截图。

      【讨论】:

        猜你喜欢
        • 2015-02-18
        • 2022-01-05
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        相关资源
        最近更新 更多