|
| 1 | +## 🚀 一个简单小巧的js测试库 |
| 2 | + |
| 3 | +<p> |
| 4 | + <a href="https://github.com/theajack/easy-test-lib"><img src="https://img.shields.io/github/stars/theajack/easy-test-lib.svg?style=social" alt="star"></a> |
| 5 | + <a href="https://theajack.gitee.io"><img src="https://img.shields.io/badge/author-theajack-blue.svg?style=social" alt="Author"></a> |
| 6 | +</p> |
| 7 | + |
| 8 | +<p> |
| 9 | + <a href="https://www.npmjs.com/package/easy-test-lib"><img src="https://img.shields.io/npm/v/easy-test-lib.svg" alt="Version"></a> |
| 10 | + <a href="https://npmcharts.com/compare/easy-test-lib?minimal=true"><img src="https://img.shields.io/npm/dm/easy-test-lib.svg" alt="Downloads"></a> |
| 11 | + <a href="https://cdn.jsdelivr.net/gh/theajack/easy-test-lib/dist/easy-test-lib.latest.min.js"><img src="https://img.shields.io/bundlephobia/minzip/easy-test-lib.svg" alt="Size"></a> |
| 12 | + <a href="https://github.com/theajack/easy-test-lib/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/easy-test-lib.svg" alt="License"></a> |
| 13 | + <a href="https://github.com/theajack/easy-test-lib/search?l=javascript"><img src="https://img.shields.io/github/languages/top/theajack/easy-test-lib.svg" alt="TopLang"></a> |
| 14 | + <a href="https://github.com/theajack/easy-test-lib/issues"><img src="https://img.shields.io/github/issues-closed/theajack/easy-test-lib.svg" alt="issue"></a> |
| 15 | + <a href="https://github.com/theajack/easy-test-lib"><img src="https://img.shields.io/librariesio/dependent-repos/npm/easy-test-lib.svg" alt="Dependent"></a> |
| 16 | +</p> |
| 17 | + |
| 18 | +**[English](https://github.com/theajack/easy-test-lib/blob/master/README.en.md) | [反馈错误/缺漏](https://github.com/theajack/easy-test-lib/issues/new) | [Gitee](https://gitee.com/theajack/easy-test-lib)** |
| 19 | + |
| 20 | + |
| 21 | +### 1. 特性 |
| 22 | + |
| 23 | +1. typescript 编写 |
| 24 | +2. 体积小巧,简单易用 |
| 25 | +3. 多端支持 |
| 26 | +4. 支持异步 |
| 27 | +5. 可自定义插件 |
| 28 | +6. 配置文件 + 命令行运行 |
| 29 | +7. 全局安装可用 |
| 30 | + |
| 31 | +### 2. 安装 |
| 32 | + |
| 33 | +#### 2.1 api调用 |
| 34 | + |
| 35 | +``` |
| 36 | +npm i easy-test-lib -D |
| 37 | +``` |
| 38 | + |
| 39 | +```js |
| 40 | +import {startTest} from 'easy-test-lib'; |
| 41 | +startTest(config); |
| 42 | +``` |
| 43 | + |
| 44 | +#### 2.2 配置文件调用 |
| 45 | + |
| 46 | +package.json 增加 |
| 47 | + |
| 48 | +``` |
| 49 | + ... |
| 50 | + "scripts": { |
| 51 | + "test": "etest <config file>" |
| 52 | + }, |
| 53 | + ... |
| 54 | +``` |
| 55 | + |
| 56 | +配置文件默认是根目录的 tc.test.js 文件,可以自由配置 |
| 57 | + |
| 58 | +根目录执行 |
| 59 | + |
| 60 | +``` |
| 61 | +npm run test |
| 62 | +``` |
| 63 | + |
| 64 | +#### 2.3 全局安装使用 |
| 65 | + |
| 66 | +``` |
| 67 | +npm i easy-test-lib -g |
| 68 | +``` |
| 69 | + |
| 70 | +配置文件与2.2中规则一致 |
| 71 | + |
| 72 | +在项目目录中运行以下命令行即可 |
| 73 | + |
| 74 | +``` |
| 75 | +etest <config file> |
| 76 | +``` |
| 77 | + |
| 78 | +### 3 配置 |
| 79 | + |
| 80 | +```js |
| 81 | +import {startTest} from 'easy-test-lib'; |
| 82 | + |
| 83 | +function add (x, y) { |
| 84 | + return x + y; |
| 85 | +} |
| 86 | + |
| 87 | +startTest({ |
| 88 | + args: { // 可选参数 |
| 89 | + // 用于传入一些公用的api,会被传入测试用例中 |
| 90 | + }, |
| 91 | + cases: [ // 测试用例配置,建议拆分文件 |
| 92 | + { |
| 93 | + name: '测试add函数', // 可选 |
| 94 | + args: { // 可选 |
| 95 | + // 当前测试用例的api |
| 96 | + }, |
| 97 | + test (args, localArgs) { |
| 98 | + // args对应公共api,localArgs对应当前测试用例api |
| 99 | + // this指代当前测试用例 |
| 100 | + return add(1 + 1); |
| 101 | + }, |
| 102 | + expect: 2, |
| 103 | + // plugin: ITestPlugin, // 当前测试用例使用的插件 可选 |
| 104 | + } |
| 105 | + ], |
| 106 | + onTestComplete (result) { // 测试全部完成回调 可选 |
| 107 | + // result 数据结构如下 |
| 108 | + /* |
| 109 | + passed: boolean; |
| 110 | + results: [ |
| 111 | + { |
| 112 | + passed: boolean; |
| 113 | + result: any; |
| 114 | + expect?: any; |
| 115 | + name?: string; |
| 116 | + index: number; |
| 117 | + time: number; |
| 118 | + } |
| 119 | + ]; |
| 120 | + time: number; |
| 121 | + */ |
| 122 | + }, |
| 123 | + onTestSingle (result) { // 单个测试用例完成回调 可选 |
| 124 | + // result 数据结构如下 |
| 125 | + /* |
| 126 | + passed: boolean; |
| 127 | + result: any; |
| 128 | + expect?: any; |
| 129 | + name?: string; |
| 130 | + index: number; |
| 131 | + time: number; |
| 132 | + */ |
| 133 | + }, |
| 134 | + // plugin: ITestPlugin, // 全局插件 可选 |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +### 4 插件 |
| 139 | + |
| 140 | +#### 4.1 内置插件 |
| 141 | + |
| 142 | +easy-test-lib 内置了 默认插件(defaultPlugin)和 异步插件(asyncPlugin) |
| 143 | + |
| 144 | +默认使用默认插件,如需使用异步插件可以直接使用字符串引入 |
| 145 | + |
| 146 | +```js |
| 147 | +plugin: 'asyncPlugin' |
| 148 | +``` |
| 149 | + |
| 150 | +或从 easy-test-lib 中引入 |
| 151 | + |
| 152 | +```js |
| 153 | +import {asyncPlugin} from 'easy-test-lib'; |
| 154 | + |
| 155 | +... |
| 156 | +plugin: asyncPlugin |
| 157 | +``` |
| 158 | + |
| 159 | +以下是一个使用异步插件的测试用例 |
| 160 | + |
| 161 | +```js |
| 162 | +function timeout (time) { |
| 163 | + return new Promise(resolve => { |
| 164 | + setTimeout(() => { |
| 165 | + resolve(true); |
| 166 | + }, time); |
| 167 | + }); |
| 168 | +} |
| 169 | + |
| 170 | +const asyncCase = { |
| 171 | + args: {aa: 22}, |
| 172 | + plugin: 'asyncPlugin', |
| 173 | + name: '测试async', |
| 174 | + async test (args: any) { |
| 175 | + await timeout(2000); |
| 176 | + console.log(args, this.args); |
| 177 | + return [ |
| 178 | + 11 |
| 179 | + ]; |
| 180 | + }, |
| 181 | + expect: [ |
| 182 | + 11 |
| 183 | + ] |
| 184 | +}; |
| 185 | + |
| 186 | +export default asyncCase; |
| 187 | +``` |
| 188 | + |
| 189 | +运行测试用例 |
| 190 | + |
| 191 | +```js |
| 192 | +import {startTest} from 'easy-test-lib'; |
| 193 | +import testAsync from './test-async'; |
| 194 | + |
| 195 | +startTest({ |
| 196 | + cases: [ |
| 197 | + testAsync |
| 198 | + ], |
| 199 | + onTestComplete (result) { |
| 200 | + console.log(`总耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`); |
| 201 | + console.log(result); |
| 202 | + }, |
| 203 | + onTestSingle (result) { |
| 204 | + console.log(`${result.index}: 耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`); |
| 205 | + } |
| 206 | +}); |
| 207 | +``` |
| 208 | + |
| 209 | +#### 4.2 自定义插件 |
| 210 | + |
| 211 | +easy-test-lib 支持自定义插件,交给开发者定制测试计算过程,一个简单的自定义插件模板如下 |
| 212 | + |
| 213 | + |
| 214 | +```js |
| 215 | +const plugin: ITestPlugin = ({ |
| 216 | + name, test, expect, args |
| 217 | +}, argsConfig) => { |
| 218 | + |
| 219 | + // do something ... |
| 220 | + |
| 221 | + return { |
| 222 | + result: {}, |
| 223 | + expect: {}, |
| 224 | + passed: true, |
| 225 | + }; |
| 226 | +}; |
| 227 | + |
| 228 | +export default plugin; |
| 229 | +``` |
| 230 | + |
| 231 | +### 5 API |
| 232 | + |
| 233 | +#### 5.1 startTest |
| 234 | + |
| 235 | +参见 上文 3 |
| 236 | + |
| 237 | +#### 5.2 isValueEqual |
| 238 | + |
| 239 | +判断两个对象是否相等,支持引用类型 |
| 240 | + |
| 241 | +引用类型会遍历其中的所有属性值是否相等 |
| 242 | + |
| 243 | +```js |
| 244 | +import {isValueEqual} from 'easy-test-lib'; |
| 245 | +console.log(isValueEqual(1, 1)); |
| 246 | +``` |
| 247 | + |
| 248 | +#### 5.3 defaultPlugin |
| 249 | + |
| 250 | +默认的插件 |
| 251 | + |
| 252 | +#### 5.4 defaultPlugin |
| 253 | + |
| 254 | +支持异步的插件 |
| 255 | + |
| 256 | +#### 5.5 ts 接口 |
| 257 | + |
| 258 | + 1. ITestConfigItem |
| 259 | + 2. ITestPlugin |
| 260 | + 3. IStartTest |
| 261 | + 4. IIsValueEqual |
0 commit comments