1 Star 0 Fork 57

skymysky / UnitAuto

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

UnitAuto

☀️ 机器学习零代码单元测试平台

零代码、全方位、自动化 测试 方法/函数 的正确性、可用性和性能

使用文档 视频教程 在线体验


机器学习单元测试平台,零代码、全方位、自动化 测试 方法/函数 的正确性、可用性和性能。
腾讯 IEG(互动娱乐事业群)、WXG(微信事业群) 两大事业群多个部门的多个项目以及快手广告使用中。

已被 互联网教育智能技术及应用国家工程实验室 收录。 https://github.com/TommyLemon/APIAuto/issues/19

image image image

特点优势

相比 JUnit, JTest, Mockito, Mockk 等一堆 Compiling testing 工具:
1.其它工具需要每个方法都写一大堆测试代码,需要开发成本、需要解决测试代码的 bug、业务代码更改后需要同步修改测试代码等;
UnitAuto 不需要写任何代码,直接读取方法的属性,自动注入参数,拿到返回值和类成员变量,机器学习自动化校验。

2.UnitAuto 这种 Runtime testing 工具无需 Mock 环境(Application, Context 等),
更不用为 无法有效地 Mock 环境相关类、第三方登录未提供 Mock 支持 等而头疼,
只要被测方法满足 有 return 值、有 interface 回调、改变成员变量 field 这 3 点中至少一点就能测。

质效无双线上技术访谈-零代码智能测试工具实践介绍-第11期

https://testwo.cn1.quickconnect.cn/vs/sharing/iiP8VK1C#!aG9tZV92aWRlby0xMQ== https://testwo.cn1.quickconnect.cn/vs/sharing/iiP8VK1C#!aG9tZV92aWRlby0xMQ==



原理说明

被测项目不需要写任何单元测试代码(逻辑代码、注解代码等全都不要),
UnitAuto 会自动生成测试参数,并执行方法,拿到返回值等进行校验。
泛型、接口等自动模拟,异步执行方法自动记录回调过程,都是零代码。

UnitAuto-Admin 是类似 APIAuto 的机器学习零代码测试工具(查看、上传、执行、测试 后端项目中的方法等),
UnitAuto-Java 提供扫描所有方法、执行某个方法两个 API (主要引入 MethodUtil.java 这个类,里面已实现)。

通过扫描项目中的可访问的 package, class, method 来生成和导入测试用例


HTTP 远程调用被测服务/App,转至内部 构造/获取 实例来 invoke 动态执行方法


获取参数、成员变量等前后状态,拦截方法被调用过程并可视化展示


通过简单配置自动调整参数组合,拿到返回结果后通过 前后结果对比 或 机器学习校验模型 来自动断言


后端不再需要像以下示例一样编写和维护大量单元测试用例代码(逻辑代码、注解代码等):


客户端可作为 HTTP Server 来为 UnitAuto 网页工具 提供远程调用的接口,效果同样:



示例项目

UnitAuto Java 后端 Server 在线 测试
APIJSON Java 后端 Server 在线 测试
UnitAuto Android 客户端 App 直接 下载 (第一次可能失败,返回报错 JSON,一般重试一次就可以)
APIJSON Android 客户端 App 直接 下载 (第一次可能失败,返回报错 JSON,一般重试一次就可以)

演讲视频

UnitAuto-机器学习自动化单元测试平台简介
https://www.bilibili.com/video/BV1Tk4y1R7Yo

UnitAuto-异步回调方法的零代码单元测试
https://www.bilibili.com/video/BV1kk4y1z7bW

集成到被测项目

Java 后端 Server

1.依赖 unitauto.jar

放到你 启动 Application 所在项目的 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 代码块 中初始化

    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<JSONObject> listener)

参考 DemoController

4.配置环境相关类及自定义处理逻辑

拦截 MethodUtil 类中的 INSTANCE_GETTER, JSON_CALLBACK, CLASS_LOADER_CALLBACK 等 interface 的回调方法,
可以支持获取 Context 等环境相关类的实例、转 JSON 对象时过滤特定类、其它自定义逻辑处理,参考 DemoApplication


Android 客户端 App

1.依赖 UnitAuto-Apk

UnitAuto-Apk 导入到你项目 app moudule 所在目录settings.gradle

include ':UnitAuto-Apk'

app moudule 目录build.gradle

dependencies {
    api project(':UnitAuto-Apk')
}

2.初始化 UnitAuto

Application onCreate 方法 中初始化

    @Override
    public void onCreate() {
        super.onCreate();
        UnitAutoApp.init(this);
    }

3.提供 UnitAuto 管理界面入口

AndroidManifest.xml 中注册 UnitAutoActivity

<manifest ... >
    <application ... >
      
        <activity
            android:name="unitauto.apk.UnitAutoActivity"
            android:launchMode="singleInstance">
        </activity>
      
     </application>
</manifest>

可在你项目的任何界面新增一个按钮或其它形式的入口,仅 DEBUG 模式下展示

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickUnit"
        android:text="UnitAutoActivity"
        android:textAllCaps="false"
        />

参考 layout/activity_main

点击这个入口跳转到 UnitAutoActivity

    public void onClickUnit(View v) {
        startActivity(UnitAutoActivity.createIntent(this));
    }

参考 MainActivity

4.配置环境相关类及自定义处理逻辑

Application onCreate 方法 中配置,参考 Java 后端 Server 中 4.配置环境相关类及自定义处理逻辑

部署后台管理工具

可以直接下载源码解压后用浏览器打开 UnitAuto-Admin/index.html,建议用 Chrome 或 火狐 (Safari、Edge、IE 等可能有兼容问题)。
也可以直接访问官网的环境 http://apijson.cn/unithttp://apijson.cn:8081

点右上角设置项 "项目服务器地址 URL",把输入框内基地址改为你主机的地址(例如 http://192.168.0.102:8080 )

右上角登录的默认管理员账号为 13000082001 密码为 123456,

然后点右上角设置项 "查看、同步方法文档",确保被测项目已启动,然后一键导入项目中的方法。
等它完成后自动显示测试用例列表,点击列表项进去查看详情和手动测试,或者一键自动回归测试全部 方法/函数。

如果测试 Android/iOS App,需要保证 手机/平板 与 使用 UnitAuto-Admin 网页的电脑 连接同一个局域网,
如果使用 Android/iOS 模拟器,则一定都是在同一个局域网,并且可以用 http://localhost:端口 进行访问。



自动管理测试用例 这个功能 需要部署 APIJSONBoot ,见
https://github.com/APIJSON/APIJSON-Demo/tree/master/APIJSON-Java-Server

远程扫描方法

UnitAuto-Admin 登录后点击 设置项 [查看、同步方法文档],等返回方法属性 JSON 后点 [上传] 按钮 image

对应发送 HTTP 请求
POST /method/list

{
    "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 点击 [运行方法] image

同步方法
unitauto.test.TestUtil.divide

{
    "static": true,
    "methodArgs": [
        {   // 可省略来自动判断的 type : Boolean,Integer,BigDecimal,String,Object,JSONArray 这几种 JSON 类型
            "type": "double",
            "value": 1
        },
        {   // 支持各种类型,例如 unitauto.demo.domain.User, List<String>, annotation.Annotation[],未注册的要写完整全路径
            "type": "double",
            "value": 2
        }
    ]
}

也可以简化为

{
    "static": true,
    "methodArgs": [
        "double:1",
        "double:2"  // 如果是 JSON 类型,可以不写类型只写值,例如 true, 1, 3.14, "ok", {"a": 1}, [1, 2, 3]
    ]
}

对应发送 HTTP 请求
POST /method/invoke

{
    "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)

    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

image

异步回调方法
unitauto.test.TestUtil.computeAsync

{
    "static": true,
    "methodArgs": [
        "long:8",
        "long:2",
        {
            "type": "unitauto.test.TestUtil$Callback",
            "value": {
                "sort()": { // 可以简写为 "sort()": false 
                    "type": "Boolean",
                    "return": false
                },
                "setData(D)": {
                    "callback": true
                }
            }
        }
    ]
}

对应调用 Java 方法
unitauto.test.TestUtil.divide(double, double)

	public static Number computeAsync(long a, long b, Callback callback) {

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(3000);
				} catch (Exception e) {}
				
				Boolean sort = callback.sort();
				if (sort != null && sort && a > b) {
					callback.minusAsId(b, a);
				} else {
					callback.minusAsId(a, b);
				}

				callback.setData("Mock inner interface success!");
			}
		}).start();

		return callback.getId();
	}

https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/test/TestUtil.java#L95-L117



详细说明见 MethodUtil.invokeMethod 的注释
https://github.com/TommyLemon/UnitAuto/blob/master/UnitAuto-Java/src/main/java/unitauto/MethodUtil.java#L353-L424


后台 Headless 无 UI 模式回归测试

Jenkins、蓝盾 等 CI/CD 等流水线不支持带 UI 测试,所以提供了这个模式,
通过调用 HTTP API 即可执行用例和查看进度,方便集成到 CI/CD 流水线。 image image

1.配置 Node 环境及 NPM 包管理工具

https://nodejs.org

2.安装相关依赖

https://koajs.com

nvm install 7
npm i koa

3.使用后台 HTTP 服务

先启动 HTTP 服务

cd js
node server.js

如果运行报错 missing package xxx,说明缺少相关依赖,参考步骤 2 来执行

npm i xxx

然后再启动 HTTP 服务。

启动成功后会有提示,点击链接或者复制到浏览器输入框打开即可。

如果托管服务是用 APIJSONBoot-MultiDataSource 部署的,
链接 host 后可以加上 /unit,例如 http://localhost:3001/unit/test/start
通过这个接口来放宽前端执行时查询测试用例、参数配置等列表的条数,一次可批量执行更多用例。



常见问题

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/APIAuto/issues/12

4.apijson.cn 访问不了

托管服务地址改为 http://47.98.196.224:8080
https://github.com/TommyLemon/APIAuto/issues/13

更多常见问题
https://github.com/TommyLemon/APIAuto/issues


感谢开源

  • jsonon
  • editor.md
  • vue.js

技术交流

关于作者

https://github.com/TommyLemon
image

如果有什么问题或建议可以 去 APIAuto 提 issue,交流技术,分享经验。
如果你解决了某些 bug,或者新增了一些功能,欢迎 提 PR 贡献代码,感激不尽。

其它项目

APIJSON 🚀 腾讯零代码、全功能、强安全 ORM 库 🏆 后端接口和文档零代码,前端(客户端) 定制返回 JSON 的数据和结构

APIAuto 敏捷开发最强大易用的 HTTP 接口工具,机器学习零代码测试、生成代码与静态检查、生成文档与光标悬浮注释,集 文档、测试、Mock、调试、管理 于一体的一站式体验

SQLAuto 智能零代码自动化测试 SQL 语句执行结果的数据库工具,任意增删改查、任意 SQL 模板变量、一键批量生成参数组合、快速构造大量测试数据

apijson-doc APIJSON 官方文档,提供排版清晰、搜索方便的文档内容展示,包括设计规范、图文教程等

APIJSONdocs APIJSON 英文文档,提供排版清晰的文档内容展示,包括详细介绍、设计规范、使用方式等

apijson.org APIJSON 官方网站,提供 APIJSON 的 功能简介、登记用户、作者与贡献者、相关链接 等

APIJSON.NET C# 版 APIJSON ,支持 MySQL, PostgreSQL, SQL Server, Oracle, SQLite

apijson-go Go 版 APIJSON , 基于Go(>=1.18) + GoFrame2, 支持查询、单表增删改、权限管理等

apijson-go Go 版 APIJSON ,支持单表查询、数组查询、多表一对一关联查询、多表一对多关联查询 等

apijson-hyperf PHP 版 APIJSON,基于 Hyperf 支持 MySQL

APIJSON-php PHP 版 APIJSON,基于 ThinkPHP,支持 MySQL, PostgreSQL, SQL Server, Oracle 等

apijson-php PHP 版 APIJSON,基于 ThinkPHP,支持 MySQL, PostgreSQL, SQL Server, Oracle 等

apijson-node 字节跳动工程师开源的 Node.ts 版 APIJSON,提供 nestjs 和 typeorm 的 Demo 及后台管理

uliweb-apijson Python 版 APIJSON,支持 MySQL, PostgreSQL, SQL Server, Oracle, SQLite 等

apijson-practice BAT 技术专家开源的 APIJSON 参数校验注解 Library 及相关 Demo

Android-ZBLibrary Android MVP 快速开发框架,Demo 全面,注释详细,使用简单,代码严谨

持续更新

https://github.com/TommyLemon/UnitAuto/commits/master

我要赞赏

创作不易,右上角点 ⭐ Star 支持下本项目吧,谢谢 ^_^
https://gitee.com/TommyLemon/UnitAuto

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright ©2019 TommyLemon(https://github.com/TommyLemon/UnitAuto) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

机器学习自动化单元测试平台,零代码、全方位、自动化 测试 方法/函数 的正确性和可用性 展开 收起
JavaScript
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/skymysky/UnitAuto.git
git@gitee.com:skymysky/UnitAuto.git
skymysky
UnitAuto
UnitAuto
master

搜索帮助

14c37bed 8189591 565d56ea 8189591