带有 Hamcrest 匹配器的 TetsNG SoftAssert [英] TetsNG SoftAssert with Hamcrest matcher

查看:36
本文介绍了带有 Hamcrest 匹配器的 TetsNG SoftAssert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 TestNG 测试中使用 Hamcrest 匹配器,并专门使用软断言.我怎样才能做到这一点?我知道我可以在如下测试中使用 Hamcrest 的断言:

I want to use a Hamcrest matcher inside a TestNG test and with a soft assert specifically. How can I do this? I know that I can use Hamcrest's assertions inside a test like:

assertThat(actual, containsInAnyOrder(expected));

但我不明白如何使用 TestNG 软断言方法:

But I can't understand how can I use TestNG soft assert method like this one:

SoftAssert softAssert = new SoftAssert();

与 Hamcrest 匹配器一起使用.

together with a Hamcrest matcher.

因为我不能像 softAssert.assertThat(...)

那么,将 Hamcrest 匹配器与 TestNG 一起使用的正确方法是什么?

So, what is the correct way to use a Hamcrest matcher together with TestNG?

推荐答案

据我所知,您不能直接将来自 TestNG 的 SoftAssert 与 hamcrest 匹配器断言混合.

To the best of my knowledge, you cannot directly blend SoftAssert from TestNG with the hamcrest matcher assertions.

但是您可以使用 hamcrest 匹配器库中的 org.assertj.core.api.SoftAssertions 来尝试进行软断言.

But you can make use of org.assertj.core.api.SoftAssertions within the hamcrest matcher library for trying to do soft assertions.

javadocs SoftAssertions 有一些示例.

为了完整起见,我在这里包含了 javadoc 中的代码片段.

For the sake of completeness, I am including the code snippet from the javadocs here.

 @Test
 public void host_dinner_party_where_nobody_dies() {
   Mansion mansion = new Mansion();
   mansion.hostPotentiallyMurderousDinnerParty();
   SoftAssertions softly = new SoftAssertions();
   softly.assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
   softly.assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
   softly.assertThat(mansion.library()).as("Library").isEqualTo("clean");
   softly.assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
   softly.assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
   softly.assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
   softly.assertThat(mansion.professor()).as("Professor").isEqualTo("well kempt");
   softly.assertAll();
 }

如果你看看 SoftAssertions 代码库,您会注意到评论说它的灵感来自 Cedric 的 博客 关于软断言.

If you take a look at the SoftAssertions codebase, you would notice that the comments say that its been inspired by Cedric's blog on soft assertions.

这篇关于带有 Hamcrest 匹配器的 TetsNG SoftAssert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆