艺术套件/香槟试验
链式试验是一个Jest/MochaJS/Vitest扩展,用于将大型试验分解为较小试验的序列
较小的测试更便于写作,维护,使用,以确保代码正确. 链条 测试可以将大型测试分解为小测试的序列. 这对 用户故事测试, (中文(简体) ), 融合测试,任何测试 多个同步步骤。 。 。 .
福利
- 加速开发测试套装: 将大,慢的测试分解为一系列更小的测试. 通过改进后的代码再用来降低测试套接字码总尺寸.
- 加速测试套跑时间 : 减少测试套房整体运行时间,其中多测试共享很多设置代码. 连锁测试就像一个普遍,多层次的"设置"系统.
支持的框架: Jest, Mocha & Vitest
连锁测试同样与 贾斯特Js, (中文(简体) ), 摩诃迦叶,或 维特。 。 。 .
事实上,链式试验应与下列任何试验框架相配合:
- 使用其中之一声明测试
test(name, testFunction)或者说it(name, testFunction),以及 - 支持基于承诺的同步测试
安装
npm install @art-suite/chained-test --save-dev
使用量
基本情况
let { firstIt } = require("@art-suite/chained-test");
firstIt("should initialize to 123", () => 123)
.thenIt("should still be 123", (value) => {
expect(value).toEqual(123);
return 456;
})
.thenIt("should be 456", (value) => {
expect(value).toEqual(456);
});
你可能会注意到,这看起来像一个连锁承诺。 这是故意的!
完整示例
let { firstIt } = require("@art-suite/chained-test");
let {
auth,
createPost,
createComment,
getComments,
logOut,
} = require("./TestApp");
const aliceEmail = "[email protected]";
const postBody = "The quick brown fox jumped over the lazy dog.";
const commentBody = "Brilliant!";
// The return-result of this first test will be passed as the
// second argument to all subsequent tests in the chain.
firstIt("Alice's user story", () => auth(aliceEmail))
// In "then" tests, the test's return value is passed to the next test.
// skipped: if neither this nor any dependent tests are selected by test framework
.thenIt("lets Alice create a post", () => createPost(postBody))
// "tap" tests: ignores the test's return value. Instead it passes lastTestValue through.
// skipped: if neither this nor any dependent tests are selected by test framework
.tapIt("lets Alice create a comment", (post, alice) =>
createComment(post.id, commentBody)
)
.thenIt("lets Alice get the comments for a post", (post, alice) =>
getComments(post.id)
)
// In "softTap" tests, the test's return value is ignored.
// Instead it passes lastTestValue through to the next test.
// skipped: if not selected by test framework
.softTapIt("should have only one comment by Alice", (comments, alice) => {
expect(comments.length).toEqual(1);
expect(comments[0].userId).toEqual(alice.id);
})
.tapIt("should be able to logOut", logOut);
这将在测试框架内产生7个测试. 没有过滤器,每个都会被按顺序执行一次. 如果测试框架的过滤器没有选择这些测试,那么它们都不会执行. 如果测试框架选择了部分但并非全部测试,则将只运行所需的测试.
例如:
- 如果只是这样的话
logOut已选定,"auth alice", "create a post", "create a comment", "get post's comments", and "logOut"被执行. - 如果只是这样的话
validate alice is returned仅选择"auth alice" and "validate alice is returned"被执行.
何时使用.news test/.new it,.tap test/.tap 它或.软Tap Test/.软TapIt
- 使用
.then*当你需要通过一个测试的结果 从链条下到其他测试. - 使用
.tap*当您想要使用相同的,通过值进行多个测试时..tap*当下游测试注意测试的副作用,但不关心其直接结果时,测试也是有益的. - 用户
.softTap*当下游测试并不在乎这个测试的结果或副作用时.
一般情况下,使用 .softTap 如果你可以的话. .softTap 具有多种优势。 如果失败,不会使下游测试失败. 如果测试者跳过它, 它实际上是跳过, 你的测试套房运行更快.
否则,选择 .tap* 或者说 .then* 基于您如何需要返回值 传递到下一个测试.
API 密码
第一个 它( )
连锁试验( ) (别名)
开始连锁测试.
let { firstIt } = require("@art-suite/chained-test");
firstIt(name, test);
- 内:
- 名称: 字符串 [可选]
- 测试: () 测试结果可以是任何东西
- 选项? :
- 测试:测试函数(如果不是全局性,则需要通过)
- 跳过: 测试跳过函数( 不需要 skip 函数, 如果存在, 则使用此. skip, 但是可以在此自定义跳过)
- 出来:
- 链式试验实例
- 效果为:
- 用测试框架注册命名的测试
.then It () /.tapIt () /.softTapIt () (中文(简体) )
.when Test () /.taptest () /.softTaptest () (别名)
在测试链上添加额外的测试.
let { firstIt } = require("@art-suite/chained-test");
firstIt(name, test)
.thenIt(name, test) // test and pass return-value to the next test
.tapIt(name, test) // test and pass previous return-value through to next test
.softTapIt(name, test) // test, pass previous return-value through to next test,
// and skip if not requested by test runner.
// aliases
.thenTest(name, test)
.tapTest(name, test)
.softTapTest(name, test);
所有三种方法都有相同的签名,但效果略有不同:
- 内:
- 名称(*):字符串
- 测试(*): (最后测试结果,第一次测试结果) 测试结果可以是任何东西
- last TestResult: 上次非“ tap” 测试返回的值.
- 第一次测试结果: 从第一次测试中返回的值 它召唤.
(*) 决 定
thenTest/tapTest/softTapTest方法可以接受任意数量的参数,只要它们被排列成名称测试对。 必须有至少一对。 如果有一对以上,每一对如果被独立调用,并按顺序调用同一方法类型,则被视为-.
- 出来:
- 一个新的链式试验实例
.then*将测试值传递到链条中的下一个测试.tap*财务报告和审定财务报表.softTap*将忽略测试的回放值, 相反,他们会通过他们最后的测试结果.
- 效果为:
- 用测试框架注册命名的测试
- 当测试进行时,通过: (最后测试结果,第一次测试结果)
- 错误:
- 如果提供的测试失败:
.then*财务报告和审定财务报表.tap*将失败并导致所有后续测试被跳过.softTap*将失败,但后续测试仍会运行
- 如果不被测试者选中:
.then*财务报告和审定财务报表.tap*如果,而且只有在下游测试(任何一种测试)的情况下,测试仍然会进行 实值 由测试运行者选择..softTap*除非测试运行者明确选择,否则测试将不会运行.
如何处理失败和跳过测试
从逻辑上讲,如果链中的一步骤失败,所有下游测试都无法再完成(*). 因此,应该跳过下游测试。 以下是每个测试框架中的链式测试处理方式:
-
摩诃迦: Mocha 支持动态跳过测试
this.skip()。链式测试利用了这一点,并跳过任何由于较早的失败而无法完成的测试. -
维特 虚拟支持
context.skip()。链式测试利用了这一点,并跳过任何由于较早的失败而无法完成的测试. -
贾斯特: 在Jest中,链式测试会跳过所依赖的测试,但Jest只允许将测试标记为成功或失败. 链式测试采用最小噪音的方法. 实际失败的测试标记为 Jest 失败 。 其余的依赖性测试没有执行, 但他们在杰斯特的报导中被标为成功。 。 。 .
截止2020年11月,有 打开功能请求 Jest 支持动态跳过测试 和莫查一样
this.skip()能力.
请加入您的支持功能请求 如果你喜欢使用链式测试 与杰斯特,像我一样。 - SBD(英语:SBD)
(*):例外是:
.softTapIt/.softTestIt测试。 "软"的信号,即使测试失败,也可以继续测试.
获得魅力:测试树
你也许在问自己 我能做简单的测试吗? 我可以,也许, 分出一点点? 哦,是的,你可以做到这一点!
通过多条下游测试路径分享一些根包测试及其结果可能有意义。 主要限制是每个测试只能依赖于之前的一个测试. 然而,任何给定的测试都可以有任何数量的依赖性后续测试.
例如:
let commonRoot = firstIt(mySetup).thenTest(test1Name, test1);
commonRoot.thenTest(test2Name, test2);
commonRoot.thenTest(test3Name, test3);
当过滤测试时,所有测试都按预期进行。 这个 commonRoot 仅执行一次,而且只有在需要时:
- 如果只是这样的话
test1已选定,mySetup, test1将被执行. - 如果只是这样的话
test2已选定,mySetup, test1 and test2将被执行. - 如果只是这样的话
test3已选定,mySetup, test1 and test3将被执行. - 若为
test2财务报告和审定财务报表test3选择所有测试,mySetup, test1, test2 and test3,将被执行.
发达国家
@art-suite/chained-test 最初开发于 Imikimi.com (英语) 作为《京都议定书》 art-testbench 与Mocha一起使用的软件包。 从那时起,链式测试被考虑出自己的包,并在Mocha和Jest的支持下更新. 这项工作的部分工作得到了以下各方的支持: GenUI.com (英语)。 。 。 .