# UnitAuto
**Repository Path**: ayoubing/UnitAuto
## Basic Information
- **Project Name**: UnitAuto
- **Description**: 机器学习单元测试平台,零代码、全方位、自动化 测试 方法/函数 的性能、正确性和可用性,由腾讯工程师开发
- **Primary Language**: JavaScript
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: http://apijson.cn/unit/
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 60
- **Created**: 2022-06-24
- **Last Updated**: 2022-06-24
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
UnitAuto
☀️ 机器学习单元测试平台
零代码、全方位、自动化 测试 方法/函数 的正确性、可用性和性能
使用文档
视频教程
在线体验
---
机器学习单元测试平台,零代码、全方位、自动化 测试 方法/函数 的正确性、可用性和性能。
腾讯 IEG(互动娱乐事业群)、WXG(微信事业群) 两大事业群多个部门的多个项目使用中。
已被 互联网教育智能技术及应用国家工程实验室 收录。
https://github.com/TommyLemon/UnitAuto/issues/15
### 特点优势
相比 JUnit, JTest, Mockito, Mockk 等一堆 Compiling testing 工具:
1.其它工具需要每个方法都写一大堆测试代码,需要开发成本、需要解决测试代码的 bug、业务代码更改后需要同步修改测试代码等;
UnitAuto 不需要写任何代码,直接读取方法的属性,自动注入参数,拿到返回值和类成员变量,机器学习自动化校验。
2.UnitAuto 这种 Runtime testing 工具无需 Mock 环境(Application, Context 等),
更不用为 无法有效地 Mock 环境相关类、第三方登录未提供 Mock 支持 等而头疼,
只要被测方法满足 有 return 值、有 interface 回调、改变成员变量 field 这 3 点中至少一点就能测。
### 原理说明
被测项目不需要写任何单元测试代码(逻辑代码、注解代码等全都不要),
UnitAuto 会自动生成测试参数,并执行方法,拿到返回值等进行校验。
泛型、接口等自动模拟,异步执行方法自动记录回调过程,都是零代码。
[UnitAuto-Admin](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Admin) 是类似 [APIAuto](https://github.com/TommyLemon/APIAuto) 的机器学习零代码测试工具(查看、上传、执行、测试 后端项目中的方法等),
[UnitAuto-Java](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Java) 提供扫描所有方法、执行某个方法两个 API (主要引入 [MethodUtil.java](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/MethodUtil.java) 这个类,里面已实现)。
##### 通过扫描项目中的可访问的 package, class, method 来生成和导入测试用例
##### HTTP 远程调用被测服务/App,转至内部 构造/获取 实例来 invoke 动态执行方法
##### 获取参数、成员变量等前后状态,拦截方法被调用过程并可视化展示
##### 通过简单配置自动调整参数组合,拿到返回结果后通过 前后结果对比 或 机器学习校验模型 来自动断言
##### 后端不再需要像以下示例一样编写和维护大量单元测试用例代码(逻辑代码、注解代码等):
##### 客户端可作为 HTTP Server 来为 UnitAuto 网页工具 提供远程调用的接口,效果同样:
### 示例项目
[UnitAuto Java 后端 Server](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Java-Demo) 在线 [测试](http://apijson.org:8000/unit/)
[APIJSON Java 后端 Server](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot) 在线 [测试](http://apijson.org/unit/)
[UnitAuto Android 客户端 App](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android) 直接 [下载](https://github.com/TommyLemon/UnitAuto/releases/download/2.6.0/UnitAutoApp.apk) (第一次可能失败,返回报错 JSON,一般重试一次就可以)
[APIJSON Android 客户端 App](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Android/APIJSONTest) 直接 [下载](https://github.com/TommyLemon/UnitAuto/releases/download/2.5.0/APIJSONTest.apk) (第一次可能失败,返回报错 JSON,一般重试一次就可以)
### 演讲视频
UnitAuto-机器学习自动化单元测试平台简介
https://www.bilibili.com/video/BV1Tk4y1R7Yo
UnitAuto-异步回调方法的零代码单元测试
https://www.bilibili.com/video/BV1kk4y1z7bW
### 集成到被测项目
#### Java 后端 Server
##### 1.依赖 unitauto.jar
放到你 [启动 Application 所在项目的 libs 目录](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Java-Demo/libs),然后 Eclipse Add to Build Path 或 Idea Add as Library
https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Java-Demo/libs
##### 2.依赖 unitauto-jar.jar
如果不打 jar/war 包,则可以跳过这个步骤。
依赖方式同步骤 1。
https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Java-Demo/libs
依赖后需要在 [Application static 代码块](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java-Demo/src/main/java/unitauto/demo/DemoApplication.java) 中初始化
```java
static {
UnitAutoApp.init();
}
```
##### 3.提供接口给 UnitAuto 后台管理工具
Controller 提供两个 POST application/json 格式的 HTTP API,分别是
```
/method/list 动态扫描方法,可以单纯接收入参并转发到 MethodUtil.listMethod(String request)
/method/invoke 动态执行方法,可以单纯接收入参并转发到 MethodUtil.invokeMethod(String request, Object instance, Listener listener)
```
参考 [DemoController](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java-Demo/src/main/java/unitauto/demo/controller/DemoController.java)
##### 4.配置环境相关类及自定义处理逻辑
拦截 [MethodUtil](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/MethodUtil.java) 类中的 INSTANCE_GETTER, JSON_CALLBACK, CLASS_LOADER_CALLBACK 等 interface 的回调方法,
可以支持获取 Context 等环境相关类的实例、转 JSON 对象时过滤特定类、其它自定义逻辑处理,参考 [DemoApplication](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java-Demo/src/main/java/unitauto/demo/DemoApplication.java)
#### Android 客户端 App
##### 1.依赖 UnitAuto-Apk
把 [UnitAuto-Apk](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android/UnitAuto-Apk) 导入到你项目 [app moudule 所在目录](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android),[settings.gradle](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android/settings.gradle) 中
```groovy
include ':UnitAuto-Apk'
```
[app moudule 目录](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android/app),[build.gradle](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Android/app/build.gradle) 中
```groovy
dependencies {
api project(':UnitAuto-Apk')
}
```
##### 2.初始化 UnitAuto
在 [Application onCreate 方法](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/app/src/main/java/unitauto/demo/DemoApp.java) 中初始化
```java
@Override
public void onCreate() {
super.onCreate();
UnitAutoApp.init(this);
}
```
##### 3.提供 UnitAuto 管理界面入口
在 [AndroidManifest.xml](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/app/src/main/AndroidManifest.xml) 中注册 [UnitAutoActivity](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoActivity.java)
```xml
```
可在你项目的任何界面新增一个按钮或其它形式的入口,仅 DEBUG 模式下展示
```xml
```
参考 [layout/activity_main](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/app/src/main/res/layout/activity_main.xml)
点击这个入口跳转到 [UnitAutoActivity](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoActivity.java)
```java
public void onClickUnit(View v) {
startActivity(UnitAutoActivity.createIntent(this));
}
```
参考 [MainActivity](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/app/src/main/java/unitauto/demo/MainActivity.java)
##### 4.配置环境相关类及自定义处理逻辑
在 [Application onCreate 方法](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Android/app/src/main/java/unitauto/demo/DemoApp.java) 中配置,参考 [Java 后端 Server 中 4.配置环境相关类及自定义处理逻辑](https://github.com/TommyLemon/UnitAuto#4%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%83%E7%9B%B8%E5%85%B3%E7%B1%BB%E5%8F%8A%E8%87%AA%E5%AE%9A%E4%B9%89%E5%A4%84%E7%90%86%E9%80%BB%E8%BE%91)
### 部署后台管理工具
可以直接[下载源码](https://github.com/TommyLemon/UnitAuto/archive/master.zip)解压后用浏览器打开 [UnitAuto-Admin](https://github.com/TommyLemon/UnitAuto/tree/master/UnitAuto-Admin)/index.html,建议用 Chrome 或 火狐 (Safari、Edge、IE 等可能有兼容问题)。
也可以直接访问官网的线上环境 http://apijson.cn/unit 或开发环境 http://apijson.org:8000/unit 。
点右上角设置项 "项目服务器地址 URL",把输入框内基地址改为你主机的地址(例如 http://192.168.0.102:8080 )
右上角登录的默认管理员账号为 13000082001 密码为 123456,
然后点右上角设置项 "查看、同步方法文档",确保被测项目已启动,然后一键导入项目中的方法。
等它完成后自动显示测试用例列表,点击列表项进去查看详情和手动测试,或者一键自动回归测试全部 方法/函数。
如果测试 Android/iOS App,需要保证 手机/平板 与 使用 UnitAuto-Admin 网页的电脑 连接同一个局域网,
如果使用 Android/iOS 模拟器,则一定都是在同一个局域网,并且可以用 http://localhost:端口 进行访问。

自动管理测试用例 这个功能 需要部署APIJSON后端,见
https://github.com/APIJSON/APIJSON-Demo/tree/master/APIJSON-Java-Server
### 远程扫描方法
UnitAuto-Admin 登录后点击 设置项 \[查看、同步方法文档],等返回方法属性 JSON 后点 \[上传] 按钮

对应发送 HTTP 请求
[POST /method/list](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java-Demo/src/main/java/unitauto/demo/controller/DemoController.java#L111-L118)
```js
{
"query": 2, // 0-数据,1-总数,2-全部
"mock": true,
"package": "unitauto.test",
"class": "TestUtil",
"method": "divide",
"types": null
}
```
详细说明见 MethodUtil.listMethod 的注释
https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/MethodUtil.java#L287-L300
### 远程调用方法
UnitAuto-Admin 点击 \[运行方法]

unitauto.test.TestUtil.divide
```js
{
"static": true,
"methodArgs": [
{ // 可省略来自动判断的 type : Boolean,Integer,BigDecimal,String,Object,JSONArray 这几种 JSON 类型
"type": "double",
"value": 1
},
{
"type": "double",
"value": 2
}
]
}
```
也可以简化为
```js
{
"static": true,
"methodArgs": [
"double:1",
"double:2" // 如果是 JSON 类型,可以不写类型只写值,例如 true, 1, 3.14, "ok", {"a": 1}, [1, 2, 3]
]
}
```
对应发送 HTTP 请求
[POST /method/invoke](https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java-Demo/src/main/java/unitauto/demo/controller/DemoController.java#L120-L170)
```js
{
"package": "unitauto.test",
"class": "TestUtil",
"method": "divide",
"static": true,
"methodArgs": [
{
"type": "double",
"value": 1
},
{
"type": "double",
"value": 2
}
]
}
```
对应调用 Java 方法
unitauto.test.TestUtil.divide(double, double)
```java
public static double divide(double a, double b) {
return a / b;
}
```
https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/test/TestUtil.java#L56-L58
详细说明见 MethodUtil.invokeMethod 的注释
https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/MethodUtil.java#L353-L424
### 常见问题
#### 1.无法访问接口
Chrome 90+ 对 CORS 请求禁止携带 Cookie 或 Chrome 80-89 强制 same-site Cookie 的策略导致
https://github.com/TommyLemon/UnitAuto/issues/11
#### 2.没有生成文档
右上角设置项与被测服务实际配置不一致 等
https://github.com/Tencent/APIJSON/issues/85
#### 3.托管服务器访问不了
不能代理接口、不能展示文档、不能对断言结果纠错 等
https://github.com/TommyLemon/UnitAuto/issues/10
#### 4.apijson.org 访问不了
管理后台网页改用 http://apijson.cn/unit ,托管服务地址改为 http://47.74.39.68:9090
https://github.com/TommyLemon/UnitAuto/issues/12
更多常见问题
https://github.com/TommyLemon/UnitAuto/issues
### 技术交流
##### 关于作者
[https://github.com/TommyLemon](https://github.com/TommyLemon)
##### QQ群聊
734652054(新)
607020115(旧)
如果有什么问题或建议可以 [提ISSUE](https://github.com/TommyLemon/UnitAuto/issues) 或 加群,交流技术,分享经验。
如果你解决了某些bug,或者新增了一些功能,欢迎 [贡献代码](https://github.com/TommyLemon/UnitAuto/pulls),感激不尽。
### 其它项目
[APIJSON](https://github.com/Tencent/APIJSON) 腾讯零代码、热更新、全自动 ORM 库 🏆 后端接口和文档自动化,前端(客户端) 定制返回 JSON 的数据和结构
[APIAuto](https://github.com/TommyLemon/APIAuto) 敏捷开发最强大易用的 HTTP 接口工具,机器学习零代码测试、生成代码与静态检查、生成文档与光标悬浮注释
[APIJSON.NET](https://github.com/liaozb/APIJSON.NET) C# 版 APIJSON ,支持 MySQL, PostgreSQL, SQL Server, Oracle, SQLite
[apijson-go](https://gitee.com/tiangao/apijson-go) Go 版 APIJSON ,支持单表查询、数组查询、多表一对一关联查询、多表一对多关联查询 等
[APIJSON-php](https://github.com/xianglong111/APIJSON-php) PHP 版 APIJSON,基于 ThinkPHP,支持 MySQL, PostgreSQL, SQL Server, Oracle 等
[apijson-php](https://github.com/qq547057827/apijson-php) PHP 版 APIJSON,基于 ThinkPHP,支持 MySQL, PostgreSQL, SQL Server, Oracle 等
[apijson-node](https://github.com/kevinaskin/apijson-node) 字节跳动工程师开源的 Node.ts 版 APIJSON,提供 nestjs 和 typeorm 的 Demo
[uliweb-apijson](https://github.com/zhangchunlin/uliweb-apijson) Python 版 APIJSON,支持 MySQL, PostgreSQL, SQL Server, Oracle, SQLite 等
[APIJSONParser](https://github.com/Zerounary/APIJSONParser) 第三方 APIJSON 解析器,将 JSON 动态解析成 SQL
[APIJSON-ToDo-Demo](https://github.com/jerrylususu/apijson_todo_demo) 一个简单的 todo 示例项目,精简数据,简化上手流程,带自定义鉴权逻辑
[apijson-practice](https://github.com/vcoolwind/apijson-practice) BAT 技术专家开源的 APIJSON 参数校验注解 Library 及相关 Demo
[apijson-learn](https://github.com/rainboy-learn/apijson-learn) APIJSON 学习笔记和源码解析
### 持续更新
[https://github.com/TommyLemon/UnitAuto/commits/master](https://github.com/TommyLemon/UnitAuto/commits/master)
### 我要赞赏
创作不易,右上角点 ⭐ Star 支持下本项目吧,谢谢 ^_^
[https://gitee.com/TommyLemon/UnitAuto](https://gitee.com/TommyLemon/UnitAuto)