1 Star 0 Fork 0

theajack / easy-test-lib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

🚀 一个简单小巧的js测试库

star Author

Version Downloads Size License TopLang issue Dependent

English | 在线使用 | 反馈错误/缺漏 | Gitee

1. 特性

  1. typescript 编写
  2. 体积小巧,简单易用
  3. 多端支持
  4. 支持异步
  5. 可自定义插件
  6. 配置文件 + 命令行运行
  7. 全局安装可用

2. 安装

2.1 api调用

npm i easy-test-lib -D
const {startTest} = require('easy-test-lib');
startTest(config);

2.2 配置文件调用

package.json 增加

    ...
    "scripts": {
        "test": "etest <config file>"
    },
    ...

配置文件默认是根目录的 easy.test.js 文件,可以自由配置

根目录执行

npm run test

2.3 全局安装使用

npm i easy-test-lib -g

配置文件与2.2中规则一致

在项目目录中运行以下命令行即可

etest <config file>

2.4 CDN

<script src="https://cdn.jsdelivr.net/npm/easy-test-lib/easy-test-lib.min.js"></script>
<script>
    ETest.startTest({
        // ...
    })
</script>

3 配置

const {startTest} = require('easy-test-lib');

function add (x, y) {
    return x + y;
}

startTest({
    args: { // 可选参数
        // 用于传入一些公用的api,会被传入测试用例中
    },
    cases: [ // 测试用例配置,建议拆分文件
        {
            name: '测试add函数', // 可选
            disabled: false, // 可选 是否禁用当前用例
            args: { // 可选
                // 当前测试用例的api
            },
            test (mergedArgs) { 
                // mergedArg 为公共arg和用例arg的合并,除此之外mergedArg 中有 $global 和$local两个属性
                // this指代当前测试用例
                return add(1, 1);
            },
            expect: 2,
            // plugin: ITestPlugin, // 当前测试用例使用的插件 可选
        }
    ],
    onTestComplete (result) { // 测试全部完成回调 可选
        // result 数据结构如下
        /*
            passed: boolean;
            results: [
                {
                    passed: boolean;
                    result: any;
                    expect?: any;
                    name?: string;
                    index: number;
                    time: number;
                }
            ];
            time: number;
        */
    },
    onTestSingle (result) { // 单个测试用例完成回调 可选
        // result 数据结构如下
        /*
            passed: boolean;
            result: any;
            expect?: any;
            name?: string;
            index: number;
            time: number;
        */
    },
    // plugin: ITestPlugin, // 全局插件 可选
});

4 插件

4.1 内置插件

easy-test-lib 内置了 默认插件(defaultPlugin) 1.0.1 及之后的版本 将异步插件功能合并进了默认插件里

以下是一个使用异步的测试用例

function timeout (time) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(true);
        }, time);
    });
}

const asyncCase = {
    args: {aa: 22},
    name: '测试async',
    async test (args: any) { // 或者返回一个 Promise 对象
        await timeout(2000);
        console.log(args, this.args);
        return [
            11
        ];
    },
    expect: [
        11
    ]
};

module.exports = asyncCase;

运行测试用例

const {startTest} = require('easy-test-lib');
const testAsync = require('./test-async');

startTest({
    cases: [
        testAsync
    ],
    onTestComplete (result) {
        console.log(`总耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`);
        console.log(result);
    },
    onTestSingle (result) {
        console.log(`${result.index}: 耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`);
    }
});

4.2 自定义插件

easy-test-lib 支持自定义插件,交给开发者定制测试计算过程,一个简单的自定义插件模板如下

const plugin: ITestPlugin = (item, mergedArgs) => {
    
    // do something ...

    return {
        result: {},
        expect: {},
        passed: true,
    };
};

export default plugin;

5 API

5.1 startTest

参见 上文 3

5.2 isValueEqual

判断两个对象是否相等,支持引用类型

引用类型会遍历其中的所有属性值是否相等

const {isValueEqual} = require('easy-test-lib');
console.log(isValueEqual(1, 1));

5.3 defaultPlugin

默认的插件

5.4 ts 接口

  1. ITestConfigItem
  2. ITestPlugin
  3. IStartTest
  4. IIsValueEqual
  5. IMergedArgs
The MIT License (MIT) Copyright (c) 2019 - present Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A simple and easy-to-use testing framework 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/theajack/easy-test-lib.git
git@gitee.com:theajack/easy-test-lib.git
theajack
easy-test-lib
easy-test-lib
master

搜索帮助