断言库的比较

本文对比了三个主要的断言库:AssertJ、Strikt和Atrium。尽管AssertJ目前流行且功能丰富,但Strikt在Kotlin中的使用更为直观,而Atrium则提供了更多高级功能但相对较复杂。每个库都有其独特优势,如AssertJ的集合断言,Strikt的清晰错误消息,以及Atrium的完全类型安全的API。
摘要由CSDN通过智能技术生成

起初我并不喜欢断言库。 测试框架提供的断言是否足够尚有争议。 但是这些库提供了一种编写更接近业务语言的自定义断言的方法。 虽然意图值得称赞,但我一直以为这条路很滑。 如果有人开始编写这样的自定义断言,那么显然需要对其进行测试。 然后,什么时候停止?

但是,与测试框架提供的断言库相比,无可否认的断言库使编写断言更加流畅。 此外,我不记得最近几年有任何具有自定义声明的项目。 因此,我倾向于假定大多数开发人员具有相同的推理,并且使用那些断言库是相当安全的。

断言库的当前状态

当我开始意识到断言库时,有两个主要的竞争者:

  1. FEST断言 它是更大的FEST套件的一部分,其中包括一个非常流行的Swing测试库。 目前,FEST不再处于积极发展中。
  2. Hamcrest Hamcrest是可用于所有主要语言(Java,Python,Ruby等)的断言库。 几年前,它已成为断言的参考库。
甚至没有引用Google Truth的清单就不会完整。 但是,无论Google品牌如何,我都觉得它从未受到任何关注。

但是,两年前,我正在从事的项目团队决定将AssertJ用于断言。 我不知道为什么,而且我可能错了,但是AssertJ似乎在当今很受欢迎。 检查Github上的相应 回购信息还发现,与AssertJ相比,Hamcrest提交的内容更大,但更稀疏。 最后,AssertJ为Guava,Joda Time,Neo4J,Swing(!)和数据库提供了特定的断言。

在本文中,我想比较3个库:

  1. AssertJ-在本文中将用作参考
  2. 斯特里克特
  3. 中庭

样本模型

在下面的内容中,我将使用从AssertJ文档中不客气的模型:

data classTolkienCharacter(valname:String,
                            valrace:Race,
                            valage:Int?=null)

enumclassRace(vallabel:String){
    HOBBIT("Hobbit"),MAN("Man"),ELF("Elf"),DWARF("Dwarf"),MAIA("Maia")
}

valfrodo=TolkienCharacter("Frodo",HOBBIT,33)
valsam=TolkienCharacter("Gimli",DWARF)
valsauron=TolkienCharacter("Sauron",MAIA)
valboromir=TolkienCharacter("Boromir",MAN,37)
valaragorn=TolkienCharacter("Aragorn",MAN)
vallegolas=TolkienCharacter("Legolas",ELF,1000)
valfellowshipOfTheRing=listOf(
        boromir,
        TolkienCharacter("Gandalf",MAN),
        aragorn,
        TolkienCharacter("Sam",HOBBIT,38),
        TolkienCharacter("Pippin",HOBBIT),
        TolkienCharacter("Merry",HOBBIT),
        frodo,
        sam,
        legolas)

AssertJ的功能

要开始使用AssertJ,只需将以下依赖项添加到POM:

<dependency>
    <groupId> org.assertj </groupId>
    <artifactId> assertj-core </artifactId>
    <version> 3.11.1 </version>
    <scope> test </scope>
</dependency>

在最基本的级别上,AssertJ允许检查是否相等和相同:

@Test
fun`assertthatfrodo'snameisequaltoFrodo`(){
  assertThat(frodo.name).isEqualTo("Frodo")
}

@Test
fun`assertthatfrodoisnotsauron`(){
    assertThat(frodo).isNotSameAs(sauron)
}
Kotlin允许函数名称包含空格字符,前提是该名称由反引号分隔。 这对于断言名称非常有用。

AssertJ还对字符串提供了不同的断言:

@Test
fun`assertthatfrodo'snamestartswithFroandendswithdo`(){
    assertThat(frodo.name)
            .startsWith("Fro")
            .endsWith("do")
            .isEqualToIgnoringCase("frodo")
}

最后,在声明集合时,AssertJ确实很出色:

@Test
fun`assertthatfellowshipoftheringmembers'namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond`(){
  assertThat(fellowshipOfTheRing).extracting<String>(TolkienCharacter::name) (1)
    .doesNotContain("Sauron","Elrond")
}

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areonlyaragorn,frodo,legolasandboromir`(){
  assertThat(fellowshipOfTheRing).filteredOn{it.name.contains("o")} (2)
    .containsOnly(aragorn,frodo,legolas,boromir)
}

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areofraceHOBBIT,ELFandMAN`(){
  assertThat(fellowshipOfTheRing).filteredOn{it.name.contains("o")} (3)
    .containsOnly(aragorn,frodo,legolas,boromir)
    .extracting<String>{it.race.label}
    .contains("Hobbit","Elf","Man")
}
  1. extracting()map()类似,但在断言的上下文中
  2. 同样, filteredOn()filter()类似
  3. 可以将filteredOn()extracting()组合在一起以细化“断言管道”中的断言

默认情况下,失败的断言消息非常基本:

org.opentest4j.AssertionFailedError:
Expecting:
 <33>
to be equal to:
 <44>
but was not.

可以通过使用as()函数来改进此类消息。 它还允许引用其他对象,以便在消息中使用它们。

@Test
fun`assertthatfrodo'sageis33`(){
    assertThat(frodo.age).`as`("%s's age",frodo.name).isEqualTo(44)
}
org.opentest4j.AssertionFailedError: [Frodo's age]
Expecting:
 <33>
to be equal to:
 <44>
but was not.

特里克特的特点

Strikt是用Kotlin编写的断言库。 它的文档非常全面且可读。

Strikt是Kotlin的断言库,旨在与JUnit或Spek等测试运行程序一起使用。
没有什么可以阻止它与TestNG一起使用。

要开始使用Strikt,请将以下依赖项片段添加到POM中:

<dependency>
    <groupId> io.strikt </groupId>
    <artifactId> strikt-core </artifactId>
    <version> 0.16.0 </version>
    <scope> test </scope>
</dependency>

关于简单用法,Strikt提供了与AssertJ相同的功能。 其API几乎一对一映射:

@Test
fun`assertthatfrodo'snameisequaltoFrodo`(){
    expectThat(frodo.name).isEqualTo("Frodo")
}

@Test
fun`assertthatfrodoisnotsauron`(){
    expectThat(frodo).isNotSameInstanceAs(sauron)
}

@Test
fun`assertthatfrodostartswithFroandendswithdo`(){
    expectThat(frodo.name)
            .startsWith("Fro")
            .endsWith("do")
            .isEqualToIgnoringCase("frodo")
}

Strikt还提供关于集合的断言:

@Test
fun`assertthatfellowshipoftheringhassize9,containsfrodoandsam,anddoesnotcontainsauron`(){
  expectThat(fellowshipOfTheRing)
    .hasSize(9)
    .contains(frodo,sam)
    .doesNotContain(sauron)
}

但是,没有对应于extracting()filteredOn()的函数:因此,应该默认返回使用map()filter()

@Test
fun`assertthatfellowshipoftheringmembers'namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond`(){
  expectThat(fellowshipOfTheRing).map{it.name}
    .contains("Boromir","Gandalf","Frodo","Legolas")
    .doesNotContain("Sauron","Elrond")
}

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areonlyaragorn,frodo,legolasandboromir`(){
  expectThat(fellowshipOfTheRing.filter{it.name.contains("o")})
    .containsExactlyInAnyOrder(aragorn,frodo,legolas,boromir)
}

使用标准API不允许链接断言,因为在AssertJ中是可能的。 作为补偿,可以通过可以接受lambda的expect()函数将断言分组在一起:

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areofraceHOBBIT,ELFandMAN`(){
  expect{
    that(fellowshipOfTheRing.filter{it.name.contains("o")})
      .containsExactlyInAnyOrder(aragorn,frodo,legolas,boromir)
    that(fellowshipOfTheRing).map{it.race.label}
      .contains("Hobbit","Elf","an")
  }
}

断言失败消息比AssertJ更具描述性:

org.opentest4j.AssertionFailedError:
▼ Expect that 33:
  ✗ is equal to 44 : found 33

这确实与集合相关的断言和分组断言闪耀,确切指出断言失败的原因:

strikt.internal.opentest4j.CompoundAssertionFailure:
▼ Expect that […]:
  ✓ contains exactly the elements […] in any order
    ✓ contains TolkienCharacter(name=Aragorn, race=MAN,…
    ✓ contains TolkienCharacter(name=Frodo, race=HOBBIT…
    ✓ contains TolkienCharacter(name=Legolas, race=ELF,…
    ✓ contains TolkienCharacter(name=Boromir, race=MAN,…
    ✓ contains no further elements
▼ Expect that […]:
  ▼ ["Man", "Man", "Man", "Hobbit"…]:
    ✗ contains the elements ["Hobbit", "Elf", "an"]
      ✓ contains "Hobbit"
      ✓ contains "Elf"
      ✗ contains "an"

还可以使消息更具描述性:

@Test
fun`assertthatfrodo'sageis33`(){
    expectThat(frodo.age).describedAs("${frodo.name}'s age").isEqualTo(44)
}
org.opentest4j.AssertionFailedError:
▼ Expect that Frodo's age:
  ✗ is equal to 44 : found 33
与AssertJ的as()相反,没有可用的方法签名来传递其他对象。 但是,由于Kotlin的字符串插值功能,因此不需要。

中庭

Atrium是用Kotlin编写的另一个声明库。

Atrium旨在支持不同的API,不同的报告样式和国际化(i18n)。 Atrium的核心以及创建复杂断言的构建器都被设计为可扩展的,因此使您可以轻松地扩展或替换组件。

与AssertJ和Strikt相比,它非常强大,但也非常复杂。

第一步是选择要依赖的JAR。 中庭有多种口味:

面向中缀

Infix允许调用不带点的流畅的API:

assert(x).toBe(2)

assert(x)toBe2
动词

默认的断言动词是assert() 。 开箱即用的另外两个动词是: assertThat()check() 。 也可以创建自己的动词。

本地化

断言消息可用英语和德语提供。

根据所需的口味,需要引用不同的JAR组合。 以下代码段将使用no-infix, assert()和英语消息:

<dependency>
    <groupId> ch.tutteli.atrium </groupId>
    <artifactId> atrium-cc-en_GB-robstoll </artifactId>
    <version> 0.7.0 </version>
    <scope> test </scope>
</dependency>

基本断言看上去与AssertJ和Strikt的相似:

@Test
fun`assertthatfrodo'snameisequaltoFrodo`(){
  assert(frodo.name).toBe("Frodo")
}

@Test
fun`assertthatfrodoisnotsauron`(){
  assert(frodo).isNotSameAs(sauron)
}

但是,Atrium的API允许使用另一种完全类型安全的编写方式:

@Test
fun`assertthatfrodo'snameisequaltoFrodo2`(){
  assert(frodo){
    property(subject::name).toBe("Frodo")
  }
}

它可以根据自己的口味进行调整。 这是在String上写入相同断言的4种不同方式:

@Test
fun`assertthatfrodostartswithFroandendswithdo`(){
  assert(frodo.name)
    .startsWith("Fro")
    .endsWith("do")
    .isSameAs("Frodo")
}

@Test
fun`assertthatfrodostartswithFroandendswithdo2`(){
  assert(frodo.name){
    startsWith("Fro")
    endsWith("do")
    isSameAs("Frodo")
  }
}

@Test
fun`assertthatfrodostartswithFroandendswithdo3`(){
  assert(frodo){
    property(subject::name)
      .startsWith("Fro")
      .endsWith("do")
      .isSameAs("Frodo")
  }
}

@Test
fun`assertthatfrodostartswithFroandendswithdo4`(){
  assert(frodo){
    property(subject::name){
      startsWith("Fro")
      endsWith("do")
      isSameAs("Frodo")
    }
  }
}

作为AssertJ和Strikt,Atrium提供了一个API来对集合执行断言:

@Test
fun`assertthatfellowshipoftheringhassize9,containsfrodoandsam,anddoesnotcontainsauron`(){
  assert(fellowshipOfTheRing)
    .hasSize(9)
    .contains(frodo,sam)
    .containsNot(sauron)
}

@Test
fun`assertthatfellowshipoftheringmembers'namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond`(){
  assert(fellowshipOfTheRing.map{it.name}) (1)
    .containsNot("Sauron","Elrond") (2)  (3)
}

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areonlyaragorn,frodo,legolasandboromir`(){
  assert(fellowshipOfTheRing.filter{it.name.contains("o")}) (1)
    .contains.inAnyOrder.only.values(aragorn,frodo,legolas,boromir) (2)  (4)
}
  1. 作为Strikt,Atrium没有用于地图和过滤器的特定API。 需要依靠Kotlin的API。
  2. 可以使用经典的包含/不包含断言。
  3. 快捷断言
  4. 全面的可定制断言

我发现没有办法完善管道中的断言。 唯一的选择是调用不同的断言:

@Test
fun`assertthatfellowshipoftheringmembers'namecontaining'o'areofraceHOBBIT,ELFandMAN`(){
  valfellowshipOfTheRingMembersWhichNameContainsO=fellowshipOfTheRing.filter{it.name.contains("o")}
  assert(fellowshipOfTheRingMembersWhichNameContainsO)
    .contains.inAnyOrder.only.values(aragorn,frodo,legolas,boromir)
  assert(fellowshipOfTheRingMembersWhichNameContainsO.map{it.race.label}.distinct())
    .containsStrictly("Hobbit","Elf","Man")
}

使用这种方法,第一个失败的断言将引发异常,并使测试流程短路,从而可能不会执行其他可能失败的断言。

另外,除了创建自己的断言之外,我没有发现任何更改失败的断言消息的东西。

结论

AssertJ是一个非常完善的Java断言库。 它有一些轻微的限制,一些来自Java,一些来自API本身。

Strikt与AssertJ非常相似,但是解决了这些限制。 如果使用Kotlin,则可以将其用作嵌入式替代品。

中庭也是用Kotlin编写的,但是以相当多的复杂性为代价提供了更多的功能。

更进一步:
该帖子还提供其他语言版本:

翻译自: https://blog.frankel.ch/comparison-assertion-libraries/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值