dubzzz/fast-check

GitHub: dubzzz/fast-check

fast-check 是一个 TypeScript 编写的 JavaScript 属性测试框架,通过自动生成大量随机测试用例来发现传统单元测试难以覆盖的边界条件和逻辑缺陷。

Stars: 4814 | Forks: 204

fast-check logo

JavaScript/TypeScript 的基于属性的测试框架

Build Status npm version monthly downloads

Coverage Status (unit tests) Package quality OpenSSF Scorecard OpenSSF Best Practices

PRs Welcome License

## 入门指南 基于属性的测试的实践教程和定义:[🏁 查看教程](https://fast-check.dev/docs/tutorials/quick-start/)。或者直接在我们预配置的 [CodeSandbox](https://codesandbox.io/s/github/dubzzz/fast-check/tree/main/examples?previewwindow=tests) 上在线尝试。 基于属性的测试框架用于检查属性的真伪。属性是一个类似于这样的陈述:_对于所有 (x, y, ...),只要前置条件 precondition(x, y, ...) 成立,谓词 predicate(x, y, ...) 即为真_。 使用以下命令安装该模块:`pnpm add -D fast-check` 或 `yarn add fast-check --dev` 或 `npm install fast-check --save-dev` 在 [mocha](http://mochajs.org/) 中集成的示例: ``` import fc from 'fast-check'; // Code under test const contains = (text, pattern) => text.indexOf(pattern) >= 0; // Properties describe('properties', () => { // string text always contains itself it('should always contain itself', () => { fc.assert(fc.property(fc.string(), (text) => contains(text, text))); }); // string a + b + c always contains b, whatever the values of a, b and c it('should always contain its substrings', () => { fc.assert( fc.property(fc.string(), fc.string(), fc.string(), (a, b, c) => { // Alternatively: no return statement and direct usage of expect or assert return contains(a + b + c, b); }), ); }); }); ``` 如果测试失败,它会抛出一个红色标记。其输出应有助于诊断实现中出了什么问题。以下是一个 `contain` 实现失败的示例: ``` 1) should always contain its substrings Error: Property failed after 1 tests (seed: 1527422598337, path: 0:0): ["","",""] Shrunk 1 time(s) Got error: Property failed by returning false Hint: Enable verbose mode in order to have the list of all failing values encountered during the run ``` 与其他测试框架的集成:[ava](https://github.com/dubzzz/fast-check-examples/blob/main/test-ava/example.spec.js)、[jasmine](https://github.com/dubzzz/fast-check-examples/blob/main/test-jasmine/example.spec.js)、[jest](https://github.com/dubzzz/fast-check-examples/blob/main/test-jest/example.spec.js)、[mocha](https://github.com/dubzzz/fast-check-examples/blob/main/test/longest%20common%20substr/test.js) 和 [tape](https://github.com/dubzzz/fast-check-examples/blob/main/test-tape/example.spec.js)。 更多示例:[简单示例](https://github.com/dubzzz/fast-check/tree/main/examples)、[模糊测试](https://github.com/dubzzz/fuzz-rest-api) 以及[针对各种算法](https://github.com/dubzzz/fast-check-examples)。 实用文档: - [🏁 基于属性的测试简介与实战](https://fast-check.dev/docs/tutorials/quick-start/) - [🐣 内置 arbitraries](https://fast-check.dev/docs/core-blocks/arbitraries/) - [🔧 自定义 arbitraries](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/) - [🏃‍♂️ 基于属性的运行器 (runners)](https://fast-check.dev/docs/core-blocks/runners/) - [💥 技巧](https://fast-check.dev/docs/configuration/) - [🔌 API 参考](https://fast-check.dev/api-reference/index.html) - [⭐ Awesome fast-check](https://fast-check.dev/docs/ecosystem/) ## 为什么我应该迁移到 fast-check? fast-check 最初是为了解决我在使用其他为 JavaScript 设计的基于属性的测试框架时遇到的限制而设计的: - **类型:** 强类型且保持最新 —— *得益于 TypeScript* - **可扩展:** 简单的 `map` 方法用于派生现有的 arbitraries,同时保持收缩 (shrink) 能力 \[[更多](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#map)\] —— *某些框架要求用户提供 a->b 和 b->a 两种映射以保持 shrinker* - **可扩展:** 一种名为 `chain` 的 flatMap 操作 \[[更多](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#chain)\] —— *能够将一个 arbitrary 的输出绑定作为另一个 arbitrary 的输入,同时保持收缩功能正常工作* - **可扩展:** 使用 `fc.pre(...)` 进行前置条件检查 \[[更多](https://fast-check.dev/docs/core-blocks/properties/#example)\] —— *如果需要,可以直接在检查函数内部过滤无效条目* - **可扩展:** 使用 `fc.gen()` 轻松从测试中的假数据切换到基于属性的方式 \[[更多](https://fast-check.dev/docs/core-blocks/arbitraries/others/#gen)\] —— *在你的谓词中生成随机值* - **智能:** 在 `fc.oneof` 上具备收缩能力 \[[更多](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#oneof)\] —— *令人惊讶的是,有些框架并不支持* - **智能:** 默认带偏差 —— *默认情况下,它会同时生成较小和较大的值,这使得无需手动调整大小参数即可更容易地深入分析反例* - **调试:** 详细模式 (verbose mode) \[[更多](https://fast-check.dev/docs/configuration/custom-reports/#verbosity)\]\[[教程](https://fast-check.dev/docs/tutorials/quick-start/read-test-reports/#how-to-increase-verbosity)\] —— *启用详细模式可让故障排除更容易* - **调试:** 直接在最小反例上重放 \[[教程](https://fast-check.dev/docs/tutorials/quick-start/read-test-reports/#how-to-re-run)\] —— *无需重放整个序列,直接获取反例* - **调试:** 除了生成的值外,还支持自定义示例 \[[更多](https://fast-check.dev/docs/configuration/user-definable-values/#run-against-custom-values)\] —— *无需复制代码即可在自定义示例上运行属性测试* - **调试:** 每次谓词运行的日志记录器 \[[更多](https://fast-check.dev/docs/core-blocks/arbitraries/others/#context)\] —— *使用 fc.context 及其日志功能简化故障排除* - **独特:** 基于模型的方法 \[[更多](https://fast-check.dev/docs/advanced/model-based-testing/)\]\[[文章](https://medium.com/criteo-labs/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5)\] —— *利用基于属性测试的威力来测试 UI、API 或状态机* - **独特:** 检测代码中的竞态条件 \[[更多](https://fast-check.dev/docs/advanced/race-conditions/)\]\[[教程](https://fast-check.dev/docs/tutorials/detect-race-conditions/)\] —— *利用基于属性测试的威力,通过打乱 promise 和 async 调用的解析顺序来检测竞态* - **独特:** 简化用户定义的边界情况 \[[更多](https://fast-check.dev/docs/configuration/user-definable-values/#shrink-custom-values)\] —— *通过询问 fast-check 是否能找到更简单的边界情况来简化 bug 修复* 更多详情,请参阅上方链接中的文档。 ### 值得信赖 fast-check 已被大项目信赖多年,例如:[jest](https://github.com/jestjs/jest)、[jasmine](https://github.com/jasmine/jasmine)、[fp-ts](https://github.com/gcanti/fp-ts)、[io-ts](https://github.com/gcanti/io-ts)、[ramda](https://github.com/ramda/ramda)、[js-yaml](https://github.com/nodeca/js-yaml)、[query-string](https://github.com/sindresorhus/query-string)... ### 强大 它还被证明有助于在主要开源项目中发现 bug,例如 [jest](https://github.com/jestjs/jest)、[query-string](https://github.com/sindresorhus/query-string)... 以及[许多其他项目](https://fast-check.dev/docs/introduction/track-record/)。 ## 兼容性 以下是在无需任何 polyfills 的情况下正常使用 fast-check 的最低要求: | fast-check | node | ECMAScript 版本 | _TypeScript (可选)_ | | ---------- | ---------------------- | ---------------- | ----------------------- | | **4.x** | ≥12.17.0(1) | ES2020 | ≥5.0 | | **3.x** | ≥8(2) | ES2017 | ≥4.1(3) | | **2.x** | ≥8(2) | ES2017 | ≥3.2(4) | | **1.x** | ≥0.12(2) | ES3 | ≥3.0(4) |
更多细节... 1. 尽管版本 12.x 应该支持版本 4 将利用的大部分 ES2020 功能,但我们建议至少依赖 Node 版本 14.x,因为它支持所有目标规范。此外,我们强烈建议切换到仍受支持的 Node LTS 版本,而不要在不受支持的版本上停留过久。 2. 除非是无法通过 polyfill 实现的功能(例如与 `bigint` 相关的功能),只要您使用至少与您的 fast-check 主版本关联的最低推荐 node 版本,fast-check 的所有功能都应可用。 3. 要求 lib 或 target ≥ ES2020,或已安装 `@types/node`。 4. 要求 lib 或 target ≥ ES2015,或已安装 `@types/node`。
## 赞助商 💸 许多个人和公司为该项目提供了资金支持,向他们所有人致以巨大的感谢 💓 all sponsors 您也可以通过 [GitHub Sponsors](https://github.com/sponsors/dubzzz) 或 [OpenCollective](https://opencollective.com/fast-check/contribute) 进行捐助,成为他们中的一员。
标签:BDD, CMS安全, DNS解析, fast-check, JavaScript, MITM代理, npm包, QuickCheck, TDD, TypeScript, 代码覆盖率, 前端测试, 单元测试, 后端测试, 基于属性的测试, 威胁情报, 安全插件, 开发者工具, 开源框架, 开源项目, 持续集成, 数据可视化, 测试框架, 自动化攻击, 软件质量, 随机测试