Testng之Assert

org.testng.Assert
Assert.assertEquals
Assert.assertNotNull
Assert.assertFalse 不存在某些数据时,为true
Assert.assertTrue
Assert.fail 
Double a = 3.14;
double b = 3.14;
Double deltaA = 3.14;
double deltaB = 0.1;
assertEquals(a, b,deltaA); true = a-b 小于 deltaA false = a-b 大于等于 deltaA 
String message = "异常描述";
assertEquals(a, b, message);b为预期值 a为实际值
String[] rto1 = {
  "boolean", "BigInteger", "List",
};
String[] rto2 = {
  "List", "BigInteger", "boolean",
};
Assert.assertEqualsNoOrder(rto1, rto2); 无序校验

org.testng.asserts.SoftAssert
final SoftAssert sa = new SoftAssert();
sa.assertTrue
sa.assertAll
sa.fail("soft assert with missing root cause", hiddenExc);异常描述message重新定义

重写message消息 

String message = "My message";
SoftAssert sa = new SoftAssert();
sa.assertTrue(false, message);
sa.assertAll

重置test-message

  @Test(dataProvider = "messages")
  public void testDefaultMessage(String actualMsg, String expectedMsg) {
    try {
      final SoftAssert sa = new SoftAssert();
      sa.assertTrue(false);
      sa.assertAll(actualMsg);
      Assert.fail();
    } catch (AssertionError exc) {
      Assert.assertNotNull(exc.getMessage());
      Assert.assertTrue(exc.getMessage().startsWith(expectedMsg));
    }
  }

 重新声明校验方法

  @Test
  public void testOnSucceedAndFailureCalled() {
    final Collection<IAssert> succeed = new ArrayList<>();
    final Collection<IAssert> failures = new ArrayList<>();
    final SoftAssert sa =
        new SoftAssert() {
          @Override
          public void onAssertSuccess(IAssert assertCommand) {
            succeed.add(assertCommand);
          }

          @Override
          public void onAssertFailure(IAssert assertCommand, AssertionError ex) {
            failures.add(assertCommand);
          }
        };
    sa.assertTrue(true);
    sa.assertTrue(false);
    Assert.assertEquals(succeed.size(), 1, succeed.toString());
    Assert.assertEquals(failures.size(), 1, failures.toString());
  }

 所有异常message收口汇总

  @Test
  public void testAssertAllCount() {
    String message = "My message";
    SoftAssert sa = new SoftAssert();
    sa.assertTrue(true);
    sa.assertTrue(false, message);
    try {
      sa.assertAll();
      Assert.fail("Exception expected");
    } catch (AssertionError e) {
      String[] lines = e.getMessage().split("\r?\n");
      Assert.assertEquals(lines.length, 2);
      lines[1] = lines[1].replaceFirst(message, "");
      Assert.assertFalse(lines[1].contains(message));
    }
  }

校验抛出异常是否包含message描述 

private static final String hiddenMsg = "hidden msg";

  @Test(description = "GITHUB-1778", dataProvider = "dp")
  public void testRootCauseInSoftFail(Exception hiddenExc) {
    try {
      SoftAssert s = new SoftAssert();
      s.fail("soft assert with missing root cause", hiddenExc);
      s.assertAll();
    } catch (AssertionError soft) {
      assertThrowableContainsText(soft, hiddenMsg);
    }
  }

  @DataProvider(name = "dp")
  public Object[][] getExceptions() {
    return new Object[][] {{new Exception(hiddenMsg)}, {new Exception(new Exception(hiddenMsg))}};
  }

  private static void assertThrowableContainsText(Throwable hard, String text) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    hard.printStackTrace(pw);
    String stackTrace = sw.toString();
    Assert.assertTrue(stackTrace.contains(text));
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值