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:

<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,则可以将其用作替代产品。

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

更进一步:

  • 断言
  • 斯特里克特
  • 中庭

该帖子还提供其他语言版本:

  • Почитайтенарусском

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

nodejs断言库

nodejs断言库_断言库的比较相关推荐

  1. Python学习笔记011_模块_标准库_第三方库的安装

    容器 -> 数据的封装 函数 -> 语句的封装 类 -> 方法和属性的封装 模块 -> 模块就是程序 , 保存每个.py文件 # 创建了一个hello.py的文件,它的内容如下 ...

  2. gcc g++ Linux下动态库_静态库

    关于Unix静态库和动态库的分析 基本概念 库有动态与静态两种,动态通常用.so为后缀,静态用.a为后缀. 例如:libhello.so libhello.a 为了在同一系统中使用不同版本的库,可以在 ...

  3. python交叉编译第三方库_第三方库交叉编译

    1.事件通知库libevent ./configure --prefix=/home/arm-libevent/ --host=arm-none-linux-gnueabi CC=arm-none-l ...

  4. python断言区间_断言整数在范围内

    我认为在内部使用assertTrue进行比较不是一个好主意- 这样,您将丢失失败消息中的任何信息:AssertionError: False is not true 这一点都没有帮助,你基本上回到了& ...

  5. aix 的c库为什么都是静态库_卢卡库:若梅罗、莱万都在努力突破极限,为什么我不能做到呢...

    直播吧12月8日讯 北京时间周四凌晨的欧冠小组赛末轮比赛,国米将在主场迎战顿涅茨克矿工.目前,蓝黑军团依然保留着些许出线希望.在这场比赛之前,国米射手卢卡库接受了<法国足球>的采访,他谈论 ...

  6. python自动补全库_这个库厉害了,自动补全Python代码,节省50%敲码时间

    近日,Reddit 上的一篇帖子引起了网友的热议.帖子作者「mlvpj」称: 「我们使用深度学习完成了一个简单的项目,可以自动进行 Python 代码补全.」 根据介绍,该项目基于 LSTM 模型,训 ...

  7. [SV]SystemVerilog 断言(SVA)检查器库(OVL)

    SystemVerilog 断言(SVA)检查器库(OVL) 前言:SystemVerilog 断言(SVA)检查器库由如下两部分组成: 由检查器组成的SystemVerilog验证库(SVL),这些 ...

  8. 计算机术语中CPU是指______,试题题库_计算机基础知识考试试卷全套.doc

    试题题库_计算机基础知识考试试卷全套.doc 第一部分 计算机基础知识 (一)单项选择题 1. 世界上第一台计算机使用的物理器件是( ). A)电子管 B)继电器 C)晶体管 D)集成电路 2. 第四 ...

  9. axure如何导出原件_如何自制Axure部件库(元件库)图文教程

    本文为用户投稿,感谢小八的分享! 初学Axure的时候,直接下载了一个公共部件库就开始用了.部件库里提供了各式各样的内容,让人觉得这个世界简直太棒了!使用Axure简直太简单了!但是当你使用了一段时间 ...

  10. 任务卡_05-数据库_-MySql 高级任务

    目录 一,财务管理系统-数据库模块 1,任务概述 2,参考代码 2.1 数据准备 2.2 参考SQL语句 1. 修改表结构,在部门表中添加部门简介字段 2. 将李四的职称改为"工程师&quo ...

最新文章

  1. 山寨山寨版手机安全卫士项目
  2. python模块调用的用法_如何使用Python语言中的random模块调用方法
  3. 【错误记录】创建密钥报错 ( Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “ keyto “ 迁移到行业标准格式 PKCS12 )
  4. Pig的安装和简单使用
  5. QML for Android 加载图片资源的几种方式
  6. 莫比乌斯反演定理证明
  7. 微软宣布公开预览Dev Spaces for AKS
  8. tp3 默认模块 默认方法_您需要了解的有关默认方法的所有信息
  9. LeetCode 316. 去除重复字母 / 1081. 不同字符的最小子序列(单调栈)
  10. oracle中的in函数,Oracle中In函数的使用
  11. Pytorch —— 优化器Optimizer(二)
  12. 谷歌极速人脸、手、人体姿态分析Blaze算法家族
  13. Spring源码解析之BeanFactory
  14. CMS4.0——后知后觉
  15. Web开发中最致命的8个小错误
  16. OMRON欧姆龙Sysmac Studio软件--最新版快捷键
  17. 阿里云实践案例:使用ECS与OSS搭建个人云盘
  18. GIS的下个十年(Cary Mann, vice president, Bentley)
  19. Vue3.0教程 (一)vue脚手架安装和搭建
  20. Fragment already added解决

热门文章

  1. 2022-2027年中国实验室自动化行业市场调研及未来发展趋势预测报告
  2. HTML设置文字的格式
  3. mysql select 临时表_mysql临时表的产生
  4. 吴伯凡-认知方法论-认知的两重性
  5. JAVA实现时间换算
  6. Java使用iTextPDF生成PDF文件
  7. CentOS7服务器安装GPU显卡驱动和CUDA简单方法
  8. 中国人工智能发展的现状
  9. 计算机论文研究思路怎么写,论文的研究思路模板范文 课题研究思路怎么写
  10. 操作性定义(Operational Definition)