热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

nodejs断言库_断言库的比较

nodejs断言库起初我并不喜欢断言库。测试框架提供的断言是否足够尚有争议。但是这些库提供了一种编写更接近业务语言的自定义断言的方法。虽然意图值得称赞,但我一直以为这

nodejs断言库

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

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

断言库的当前状态

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

  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:

org.assertj assertj-core 3.11.1 test

在最基本的级别上,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&#96;assertthatfellowshipoftheringmembers&#39;namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond&#96;(){assertThat(fellowshipOfTheRing).extracting<String>(TolkienCharacter::name) (1).doesNotContain("Sauron","Elrond")
}&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areonlyaragorn,frodo,legolasandboromir&#96;(){assertThat(fellowshipOfTheRing).filteredOn{it.name.contains("o")} (2).containsOnly(aragorn,frodo,legolas,boromir)
}&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areofraceHOBBIT,ELFandMAN&#96;(){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()类似&#xff0c;但是在断言的上下文中
  2. 同样&#xff0c; filteredOn()filter()类似
  3. 可以将filteredOn()extracting()组合在一起以细化“断言管道”中的断言

默认情况下&#xff0c;失败的断言消息非常基本&#xff1a;

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

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

&#64;Test
fun&#96;assertthatfrodo&#39;sageis33&#96;(){assertThat(frodo.age).&#96;as&#96;("%s&#39;s age",frodo.name).isEqualTo(44)
}

org.opentest4j.AssertionFailedError: [Frodo&#39;s age]
Expecting:<33>
to be equal to:<44>
but was not.

斯特里克特的特点

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

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

要开始使用Strikt&#xff0c;请将以下依赖项片段添加到POM中&#xff1a;

io.strikt strikt-core 0.16.0 test

关于简单用法&#xff0c;Strikt提供了与AssertJ相同的功能。 其API几乎一对一映射&#xff1a;

&#64;Test
fun&#96;assertthatfrodo&#39;snameisequaltoFrodo&#96;(){expectThat(frodo.name).isEqualTo("Frodo")
}&#64;Test
fun&#96;assertthatfrodoisnotsauron&#96;(){expectThat(frodo).isNotSameInstanceAs(sauron)
}&#64;Test
fun&#96;assertthatfrodostartswithFroandendswithdo&#96;(){expectThat(frodo.name).startsWith("Fro").endsWith("do").isEqualToIgnoringCase("frodo")
}

Strikt还提供关于集合的断言&#xff1a;

&#64;Test
fun&#96;assertthatfellowshipoftheringhassize9,containsfrodoandsam,anddoesnotcontainsauron&#96;(){expectThat(fellowshipOfTheRing).hasSize(9).contains(frodo,sam).doesNotContain(sauron)
}

但是&#xff0c;没有对应于extracting()filteredOn()的函数&#xff1a;因此&#xff0c;应该默认返回使用map()filter() &#xff1a;

&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond&#96;(){expectThat(fellowshipOfTheRing).map{it.name}.contains("Boromir","Gandalf","Frodo","Legolas").doesNotContain("Sauron","Elrond")
}&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areonlyaragorn,frodo,legolasandboromir&#96;(){expectThat(fellowshipOfTheRing.filter{it.name.contains("o")}).containsExactlyInAnyOrder(aragorn,frodo,legolas,boromir)
}

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

&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areofraceHOBBIT,ELFandMAN&#96;(){expect{that(fellowshipOfTheRing.filter{it.name.contains("o")}).containsExactlyInAnyOrder(aragorn,frodo,legolas,boromir)that(fellowshipOfTheRing).map{it.race.label}.contains("Hobbit","Elf","an")}
}

断言失败消息比AssertJ更具描述性&#xff1a;

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

这确实与集合相关的断言和分组断言闪耀&#xff0c;确切指出断言失败的原因&#xff1a;

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

还可以使消息更具描述性&#xff1a;

&#64;Test
fun&#96;assertthatfrodo&#39;sageis33&#96;(){expectThat(frodo.age).describedAs("${frodo.name}&#39;s age").isEqualTo(44)
}

org.opentest4j.AssertionFailedError:
▼ Expect that Frodo&#39;s age:✗ is equal to 44 : found 33

与AssertJ的as&#xff08;&#xff09;相反&#xff0c;没有可用的方法签名来传递其他对象。 但是&#xff0c;由于Kotlin的字符串插值功能&#xff0c;因此无需这样做。

中庭

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

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

与AssertJ和Strikt相比&#xff0c;它非常强大&#xff0c;但也非常复杂。

第一步是选择要依赖的JAR。 中庭有多种口味&#xff1a;

面向中缀

Infix允许调用不带点的流畅的API&#xff1a;

assert(x).toBe(2)assert(x)toBe2

动词

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

本地化

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

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

ch.tutteli.atrium atrium-cc-en_GB-robstoll 0.7.0 test

基本断言与AssertJ和Strikt的非常相似&#xff1a;

&#64;Test
fun&#96;assertthatfrodo&#39;snameisequaltoFrodo&#96;(){assert(frodo.name).toBe("Frodo")
}&#64;Test
fun&#96;assertthatfrodoisnotsauron&#96;(){assert(frodo).isNotSameAs(sauron)
}

但是&#xff0c;Atrium的API允许使用另一种完全类型安全的编写方式&#xff1a;

&#64;Test
fun&#96;assertthatfrodo&#39;snameisequaltoFrodo2&#96;(){assert(frodo){property(subject::name).toBe("Frodo")}
}

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

&#64;Test
fun&#96;assertthatfrodostartswithFroandendswithdo&#96;(){assert(frodo.name).startsWith("Fro").endsWith("do").isSameAs("Frodo")
}&#64;Test
fun&#96;assertthatfrodostartswithFroandendswithdo2&#96;(){assert(frodo.name){startsWith("Fro")endsWith("do")isSameAs("Frodo")}
}&#64;Test
fun&#96;assertthatfrodostartswithFroandendswithdo3&#96;(){assert(frodo){property(subject::name).startsWith("Fro").endsWith("do").isSameAs("Frodo")}
}&#64;Test
fun&#96;assertthatfrodostartswithFroandendswithdo4&#96;(){assert(frodo){property(subject::name){startsWith("Fro")endsWith("do")isSameAs("Frodo")}}
}

作为AssertJ和Strikt&#xff0c;Atrium提供了一个API来对集合执行断言&#xff1a;

&#64;Test
fun&#96;assertthatfellowshipoftheringhassize9,containsfrodoandsam,anddoesnotcontainsauron&#96;(){assert(fellowshipOfTheRing).hasSize(9).contains(frodo,sam).containsNot(sauron)
}&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namescontainsBoromir,Gandalf,FrodoandLegolasanddoesnotcontainSauronandElrond&#96;(){assert(fellowshipOfTheRing.map{it.name}) (1).containsNot("Sauron","Elrond") (2) (3)
}&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areonlyaragorn,frodo,legolasandboromir&#96;(){assert(fellowshipOfTheRing.filter{it.name.contains("o")}) (1).contains.inAnyOrder.only.values(aragorn,frodo,legolas,boromir) (2) (4)
}

  1. 作为Strikt&#xff0c;Atrium没有用于地图和过滤器的特定API。 需要依靠Kotlin的API。
  2. 可以使用经典的包含/不包含断言。
  3. 快捷方式断言
  4. 全面的可定制断言

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

&#64;Test
fun&#96;assertthatfellowshipoftheringmembers&#39;namecontaining&#39;o&#39;areofraceHOBBIT,ELFandMAN&#96;(){valfellowshipOfTheRingMembersWhichNameContainsO&#61;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")
}

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

另外&#xff0c;除了创建自己的断言之外&#xff0c;我没有发现任何更改失败的断言消息的内容。

结论

AssertJ是一个非常全面的Java断言库。 它有一些轻微的限制&#xff0c;一些来自Java&#xff0c;一些来自API本身。

Strikt与AssertJ非常相似&#xff0c;但是解决了这些限制。 如果使用Kotlin&#xff0c;则可以将其用作替代产品。

Atrium也用Kotlin编写&#xff0c;但是以相当多的复杂性为代价提供了更多功能。

更进一步&#xff1a;

  • 断言
  • 斯特里克特
  • 中庭
该帖子还提供其他语言版本&#xff1a;
  • Почитайтенарусском

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

nodejs断言库



推荐阅读
author-avatar
书友85467040
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有