gen-test-plugin
我们日常编写代码的过程中, 经常需要为代码编写测试案例.
随着对代码质量的要求越来越高, 很多公司开始通过代码的测试覆盖率作为 QA 的一个评定指标.
本框架可以一键生成所有代码对应的 junit 测试案例, 为你的人生节约宝贵的时间.
特性
支持生成 junit4/junit5
支持 jdk7
支持自定义生成模板
更新记录
更新记录
gen-test
用于生成 Junit4/Junit5 单元测试.
引入
- <plugin>
- <groupId>com.GitHub.houbb</groupId>
- <artifactId>gen-test-plugin</artifactId>
- <version>0.0.1</version>
- </plugin>
属性说明
属性 | 说明 | 默认值 | 类型 | 备注 |
---|---|---|---|---|
isOverwriteWhenExists | 如果 test 文件已存在,是否覆盖 | false | 字符串 | 默认不进行覆盖 |
encoding | 项目编码 | utf-8 | 字符串 | |
includes | 包含文件正则 | **\/*.java | 字符串 | 默认为所有 java 文件 |
excludes | 排除文件正则 | 字符串 | 默认不进行排除 | |
junitVersion | junit 版本 | 4 | 字符串 | 默认为 junit4 |
运行
命令行直接执行
mvn com.GitHub.houbb:gen-test-plugin:0.0.1:gen-test
直接运行 [gen-test] 插件下 gen-test 即可在项目 test 目录下生成对应的测试类.
效果
比如项目有如下类:
- public class HelloTestDemo {
- public static String hello() {
- return "hello";
- }
- }
则生成对应测试文件:
- package com.GitHub.houbb.gen.maven.plugin.util;
- import org.junit.Test;
- /**
- * HelloTestDemo Tester.
- *
- * @author houbinbin
- * @since Tue Nov 14 16:01:53 CST 2017
- * @version 1.0
- */
- public class HelloTestDemoTest {
- /**
- *
- * Method: hello()
- */
- @Test
- public void helloTest() throws Exception {
- }
- }
junit5 模板
如果想自定义, 可以在 maven 项目 resources 文件夹下创建文件 junitTest5.ftl, 在下面的基础上进行修改.
junit4 模板
上述的文件生成根据如下模板 (Freemarker 语法) 生成
如果想自定义, 可以在 maven 项目 resources 文件夹下创建文件 junitTest4.ftl, 在下面的基础上进行修改.
entry
待生成的实体类信息.
${entry.packageName} 包名
${entry.className} 类名
entry.methodList 获取所有的 非私有 / 非 main() 方法列表. 暂时未提供私有方法列表, 后期可加.
today
当前日期. 后期考虑添加格式化配置.
method
方法的属性
${method.signature} 方法的签名
${method.name} 方法名称
${method.comment} 方法注释
${method.sourceCode} 方法体内的代码
- package ${entry.packageName};
- import org.junit.Test;
- /**
- * ${entry.className} Tester.
- *
- * @author houbinbin
- * @since ${today}
- * @version 1.0
- */
- public class ${entry.className}Test {
- <#if entry.methodList??>
- <#list entry.methodList as method>
- /**
- *
- * Method: ${method.signature}
- */
- @Test
- public void ${method.name}Test() throws Exception {
- }
- </#list>
- <#else>
- </#if>
- }
拓展阅读
idoc - 自动生成代码文档 https://github.com/houbb/idoc
data-factory - 自动根据 class 生成测试对象 https://github.com/houbb/data-factory
源码学习
https://github.com/houbb/gen-test-plugin 已经开源, 你可以点击链接学习相关源码.
如果有对你帮助, 可以给个 star 支持一下作者.
公众号
更多技术文章, 生活趣事, 尽在 [老马啸西风] .
来源: http://www.bubuko.com/infodetail-3354989.html