nodejs断言库
起初我并不喜欢断言库。 测试框架提供的断言是否足够尚有争议。 但是这些库提供了一种编写更接近业务语言的自定义断言的方法。 虽然意图值得称赞,但我一直以为这条路很滑。 如果有人开始编写这样的自定义断言,那么显然需要对其进行测试。 然后,什么时候停止?
但是,无可否认的是,与测试框架相比,断言库使编写断言更加流畅。 此外,我不记得最近几年有任何具有自定义声明的项目。 因此,我倾向于假定大多数开发人员具有相同的推理,并且使用那些断言库是相当安全的。
断言库的当前状态
当我开始意识到断言库时,有两个主要的竞争者:
- FEST断言 。 它是更大的FEST套件的一部分,其中包括一个非常流行的Swing测试库。 目前,FEST不再处于积极发展中。
- Hamcrest 。 Hamcrest是可用于所有主要语言(Java,Python,Ruby等)的断言库。 几年前,它成为断言的参考库。
甚至没有引用Google Truth的清单就不会完整。 但是,无论Google品牌如何,我都觉得它从未受到任何关注。
但是,两年前,我正在从事的项目团队决定将AssertJ用于断言。 我不知道为什么,而且我可能错了,但是AssertJ似乎在当今很受欢迎。 检查Github上的相应 回购协议还发现,与AssertJ相比,Hamcrest提交的内容更大,但更稀疏。 最后,AssertJ为Guava,Joda Time,Neo4J,Swing(!)和数据库提供了特定的断言。
在本文中,我想比较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")
}
-
extracting()
与map()
类似,但是在断言的上下文中 - 同样,
filteredOn()
与filter()
类似 - 可以将
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)
}
- 作为Strikt,Atrium没有用于地图和过滤器的特定API。 需要依靠Kotlin的API。
- 可以使用经典的包含/不包含断言。
- 快捷方式断言
- 全面的可定制断言
我发现没有办法完善管道中的断言。 唯一的选择是调用不同的断言:
@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,则可以将其用作替代产品。
Atrium也用Kotlin编写,但是以相当多的复杂性为代价提供了更多功能。
翻译自: https://blog.frankel.ch/comparison-assertion-libraries/
nodejs断言库