Skip to content

kequandian/zero-test

Repository files navigation

zero-test

Install

git clone https://github.com/kequandian/zero-test.git sh ./init install

Usage

Usage: node index.js <method> <api> [options] [value ...]

Options:
  --out                                           输出结果
  --report                                        输出并将结果记录日志
  --info                                          从swagger中获取api描述
  --parent                                        仅输出主表
  --head
  --tail
  --notnull                                       default option
  --all
  --table <value>                                 指定数据库表生成请求参数
  --swagger                                       从swagger中获取api所需字段信息生成请求参数
  --filter <value>                                添加或替换生成参数
  --save <field>                                  保存当前api返回的某字段值(id...), 通过#SAVE_VALUE使用该值
  -h, --help                                      output usage information

Commands:
  login <endpoint> <account> <password> [report]  登录系统保存登录信息
  server <cmd> [options]                          服务器设置
  journal <cmd> [option]                          日志设置
  pdf [options] <outputFile>                      输出日志为PDF
  test [options] <testcase> <journal-file>        直接测试并输出测试报告
Example: login api admin 111111
         server  help
         journal help
         pdf output/demo.pdf
         get api/cms/article/categories --out
         post api/cms/article/categories --filter='{"key":"value","array":[1,2,3],"items":{"key":"value"}}' --out --table=article_category
         test public/testcase/demo.tc output/demo.pdf

Journal

$ node index.js journal --help
Usage: journal [options] <cmd> [option]

Options:
  -h, --help  output usage information

Usage:
   journal ls
   journal current
   journal set <journal-file>
   journal rm <journal-file>
   journal rewrite

PDF

$ node index.js pdf --help
Usage: pdf [options] <outputFile>

Options:
  --target <target_file>  指定需转换成pdf的原文件, 不进行指定则默认转换当前所选日志文件
  -h, --help              output usage information

Usage:
   pdf output/testcase.pdf
   pdf output/testcase.pdf --target=public/logs/testcase

testcase

Usage: test [options] <testcase> <journal-file>

多api组合测试

Options:
  -f, --force  执行整个testcase,不被错误返回所打断
  -h, --help   output usage information

Usage:
   test demo/testcase demo/testcase.pdf

How to use

Base

  1. 安装
$ ./init install
  1. 配置子工作目录 (可选)
$ ./init map ‪C:/Users/10238/Desktop/
map zero-test to C:/Users/10238/Desktop/ ...
done!
  1. 修改配置文件(若使用子工作目录,进行相应切换即可)
$ vim config/server.config
module.exports = {
    host: 'http://127.0.0.1:8080/',
    mysql: {
        host : '127.0.0.1',
        port : 3306,
        database : 'env_test',
        user : 'root',
        password : 'root'
    }
};

---------------- OR ----------------
$ node index.js server --help
Usage: server [options] <host> <port>

Options:
  -h, --help  output usage information

$ node index.js mysql --help
Usage: mysql [options] <opt> [argv1] [argv2]

mysql <host|database|user> [argv1] [argv2]

Options:
  -h, --help  output usage information
Example: mysql set host 127.0.0.1 3306
         mysql set database zero-test
         mysql set user root root


  1. 设置日志(设置日志文件,清空文件内容)
$ node index.js journal set testcase
$ node index.js journal rewrite
  1. 登录(api访问请求头需要带上Authorization时可选)
$ node index.js login sys admin 111111
  1. 调用api并输出
$ node index.js post api/eav/entities --filter='{"entityName":"E1"}' --out

post--api/eav/entities
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"entityName":"E1",... │ 10   │ 1     |
+---------+-------+-------------------------+------+-------+
+-----------------------------------+
|           data#records            |
+─────+────+────────────+───────────+
| row │ id │ entityName │ tableName |
+─────┼────┼────────────┼───────────+
| 0   │ 3  │ E1         │           |
+-----+----+------------+-----------+
  1. 获取列表api第一条数据id并查询其详情(即相当于调用get api/eav/entities/3), 并将返回字段id的值保存
$ node index.js get api/eav/entities --head --save=id --out

get--api/eav/entities
+-----------------+
|      data       |
+────+────────────+
| id │ entityName |
+────┼────────────+
| 3  │ E1         |
+----+------------+
  1. 通过保存值调用api, 并保存字段entityName的值
$ node index.js get api/eav/entities/#SAVE_VALUE --save=entityName --out

get--api/eav/entities/3
+-----------------+
|      data       |
+────+────────────+
| id │ entityName |
+────┼────────────+
| 3  │ E1         |
+----+------------+
  1. 通过保存值post数据
$ node index.js post api/eav/entities --filter='{"entityName":"#SAVE_VALUE"}' --out

post--api/eav/entities
+--------------------------+
|           data           |
+──────+────────+──────────+
| code │ errors │ message  |
+──────┼────────┼──────────+
| 4007 │ []     │ 重复键值 |
+------+--------+----------+

执行的sql为

==>  Preparing: INSERT INTO t_eav_entity ( entity_name ) VALUES ( ? ) 
==> Parameters: E1(String)
  1. 单post调用并将结果记录日志
$ node index.js post api/eav/entities --filter='{"entityName":"E2"}' --report

post--api/eav/entities
+------------------------+
|          data          |
+──────+──────+──────────+
| code │ data │ message  |
+──────┼──────┼──────────+
| 200  │ 1    │ 操作成功 |
+------+------+----------+
  1. 调用GET请求并记录
$ node index.js get api/eav/entities --report

get--api/eav/entities
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"entityName":"E1",... │ 10   │ 2     |
+---------+-------+-------------------------+------+-------+
+-----------------------------------+
|           data#records            |
+─────+────+────────────+───────────+
| row │ id │ entityName │ tableName |
+─────┼────┼────────────┼───────────+
| 0   │ 4  │ E1         │           |
+─────┼────┼────────────┼───────────+
| 1   │ 5  │ E2         │           |
+-----+----+------------+-----------+
  1. 将日志中记录的内容导出pdf
$ node index.js pdf demo/testcase.pdf
converting pdf from public/logs/testcase to demo/testcase.pdf
Done

Testcase

  1. 编写testcase
$ cat demo/testcase_demo

## 组合api测试
journal set testcase
journal rewrite
# set journal testcase
# journal rewrite

# 管理员登录
login sys admin 111111
# 获取组织列表
get api/sys/org

# 组织A用户登录
login sys user1234 111111
# 获取组织列表
get api/sys/org

# 组织A1用户登录
login sys user12341 111111
# 获取组织列表
get api/sys/org

# 组织B用户登录
login api user12345 111111
# 获取组织列表
get api/sys/org
  1. 执行 testcase
$ ./index.js test public/testcase/demo.tc --journal output/demo.pdf
testcase running...

## 组合api测试
# set journal testcase
# journal rewrite

# 管理员登录
login api admin 111111 report
# 获取组织列表
get api/sys/org

export report: demo/testcase_demo.pdf
converting pdf from public/logs/testcase to demo/testcase_demo.pdf
Done

Demo

Base

$ ./index.js get api/cms/article/categories --out
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"allowImage":100,"... │ 10   │ 4     |
+---------+-------+-------------------------+------+-------+
+------------------------------------------------------+
|                     data#records                     |
+─────+────────────+───────────+────+─────────+────────+
| row │ allowImage │ fastEntry │ id │  name   │ typeId |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 0   │ 100        │ 9         │ 11 │ zy      │ 7057   |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 1   │ 44         │ 38        │ 24 │ hXRr>   │ 117    |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 2   │ 23         │ 104       │ 25 │ zli.<-3 │ 49     |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 3   │ 63         │ 93        │ 26 │ !H      │ 36     |
+-----+------------+-----------+----+---------+--------+
$ ./index.js post api/cms/article/categories --out --table=article_category
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"allowImage":100,"... │ 10   │ 5     |
+---------+-------+-------------------------+------+-------+
+-------------------------------------------------------------------------------------------------------------+
|                                                data#records                                                 |
+─────+────────────+───────────+────+────────────────────────────────────────────────────────────────+────────+
| row │ allowImage │ fastEntry │ id │                              name                              │ typeId |
+─────┼────────────┼───────────┼────┼────────────────────────────────────────────────────────────────┼────────+
| 0   │ 100        │ 9         │ 11 │ zy                                                             │ 7057   |
+─────┼────────────┼───────────┼────┼────────────────────────────────────────────────────────────────┼────────+
| 1   │ 44         │ 38        │ 24 │ hXRr>                                                          │ 117    |
+─────┼────────────┼───────────┼────┼────────────────────────────────────────────────────────────────┼────────+
| 2   │ 23         │ 104       │ 25 │ zli.<-3                                                        │ 49     |
+─────┼────────────┼───────────┼────┼────────────────────────────────────────────────────────────────┼────────+
| 3   │ 63         │ 93        │ 26 │ !H                                                             │ 36     |
+─────┼────────────┼───────────┼────┼────────────────────────────────────────────────────────────────┼────────+
| 4   │ 1          │ 0         │ 27 │ 8sMSk?&r~iV^_Sqw76*IcE#PIbq1>!GA7Mh*7boxYkTVdGI_KrpEc5z^A?b255 │ 29727  |
+-----+------------+-----------+----+----------------------------------------------------------------+--------+

$ ./index.js put api/cms/article/categories --out --table=article_category --tail --filter='{name:test}'
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"allowImage":100,"... │ 10   │ 5     |
+---------+-------+-------------------------+------+-------+
+------------------------------------------------------+
|                     data#records                     |
+─────+────────────+───────────+────+─────────+────────+
| row │ allowImage │ fastEntry │ id │  name   │ typeId |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 0   │ 100        │ 9         │ 11 │ zy      │ 7057   |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 1   │ 44         │ 38        │ 24 │ hXRr>   │ 117    |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 2   │ 23         │ 104       │ 25 │ zli.<-3 │ 49     |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 3   │ 63         │ 93        │ 26 │ !H      │ 36     |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 4   │ 1          │ 0         │ 27 │ test    │ 6283   |
+-----+------------+-----------+----+---------+--------+
$ ./index.js delete api/cms/article/categories --out --tail
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"allowImage":100,"... │ 10   │ 4     |
+---------+-------+-------------------------+------+-------+
+------------------------------------------------------+
|                     data#records                     |
+─────+────────────+───────────+────+─────────+────────+
| row │ allowImage │ fastEntry │ id │  name   │ typeId |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 0   │ 100        │ 9         │ 11 │ zy      │ 7057   |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 1   │ 44         │ 38        │ 24 │ hXRr>   │ 117    |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 2   │ 23         │ 104       │ 25 │ zli.<-3 │ 49     |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 3   │ 63         │ 93        │ 26 │ !H      │ 36     |
+-----+------------+-----------+----+---------+--------+

Report

$ ./index.js get api/cms/article/categories --report
+----------------------------------------------------------+
|                           data                           |
+─────────+───────+─────────────────────────+──────+───────+
| current │ pages │ records                 │ size │ total |
+─────────┼───────┼─────────────────────────┼──────┼───────+
| 1       │ 1     │ [{"allowImage":100,"... │ 10   │ 4     |
+---------+-------+-------------------------+------+-------+
+------------------------------------------------------+
|                     data#records                     |
+─────+────────────+───────────+────+─────────+────────+
| row │ allowImage │ fastEntry │ id │  name   │ typeId |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 0   │ 100        │ 9         │ 11 │ zy      │ 7057   |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 1   │ 44         │ 38        │ 24 │ hXRr>   │ 117    |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 2   │ 23         │ 104       │ 25 │ zli.<-3 │ 49     |
+─────┼────────────┼───────────┼────┼─────────┼────────+
| 3   │ 63         │ 93        │ 26 │ !H      │ 36     |
+-----+------------+-----------+----+---------+--------+

$ ./index.js pdf public/logs/2018-12-25.log demo.pdf
converting pdf from public/logs/2018-12-25.log to demo.pdf
Done

Testcase

$ ./index.js test public/testcase/demo.tc -j output/demo.pdf

url-pdf

说明

About

简化api测试的cli工具。支持json数据表格化与测试报表pdf导出

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors