|
| 1 | +--- |
| 2 | +title: codingapi-test测试框架 |
| 3 | +permalink: /docs/codingapi-test/ |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +## 单元测试介绍 |
| 8 | + |
| 9 | + |
| 10 | +### 一般单元测试要达到的效果: |
| 11 | +* 检验代码是否可以正常工作 |
| 12 | +* 要达到业务层率覆盖率100% |
| 13 | +* 不依赖其他模块与数据可独立运行 |
| 14 | +* 执行测试以后不允许产生脏数据影响业务 |
| 15 | +* 要对业务产生的影响做检验确认 |
| 16 | + |
| 17 | + |
| 18 | +## codingapi-test框架 |
| 19 | + |
| 20 | +框架基于Springboot2.x研发,可快速准备数据、校验数据、清理数据的测试工具。 |
| 21 | + |
| 22 | +使用codingapi-test框架的理由: |
| 23 | +* 方便快捷的准备测试数据 |
| 24 | +* 自动化的校验 |
| 25 | +* 自动化的清理数据 |
| 26 | +* 兼容MongoDB、MySQL(关系型) |
| 27 | + |
| 28 | +github: |
| 29 | + |
| 30 | +[https://github.com/codingapi/codingapi-test](https://github.com/codingapi/codingapi-test) |
| 31 | + |
| 32 | +maven: |
| 33 | +``` |
| 34 | +<dependency> |
| 35 | + <groupId>com.codingapi</groupId> |
| 36 | + <artifactId>codingapi-test</artifactId> |
| 37 | + <version>0.0.1</version> |
| 38 | +</dependency> |
| 39 | +``` |
| 40 | + |
| 41 | +## 配置说明 |
| 42 | + |
| 43 | +`@TestMethod`提供了对单元测试辅助功能。 |
| 44 | +1、导入测试数据。 |
| 45 | +2、检查确认数据 |
| 46 | +3、数据清理 |
| 47 | + |
| 48 | +```java |
| 49 | +@Test |
| 50 | +@TestMethod( |
| 51 | + //是否开启导入数据 |
| 52 | + enablePrepare = true, |
| 53 | + //导入数据的文件 可以是数组 |
| 54 | + prepareData = {"t_demo.xml"}, |
| 55 | + //是否开启检查 |
| 56 | + enableCheck = true, |
| 57 | + //检查数据项目 |
| 58 | + checkMongoData ={ |
| 59 | + //MongoDB数据检查 |
| 60 | + @CheckMongoData( |
| 61 | + //关键字质 |
| 62 | + primaryVal = "user:123", |
| 63 | + //查询关键字 |
| 64 | + primaryKey = "info", |
| 65 | + //关键值类型 |
| 66 | + type = CheckMongoData.Type.String, |
| 67 | + //错误提示 |
| 68 | + desc = "数据不存在", |
| 69 | + //加载类对象 |
| 70 | + bean = Logger.class, |
| 71 | + //校验数据 key:字段,value:值,type:值类型 |
| 72 | + expected = @Expected(key = "id",value = "1",type = Expected.Type.Long)) |
| 73 | + }, |
| 74 | + checkMysqlData = { |
| 75 | + //Mysql 数据检查 |
| 76 | + @CheckMysqlData( |
| 77 | + //执行sql |
| 78 | + sql = "select name from t_demo where name = '123'", |
| 79 | + //错误提示 |
| 80 | + desc = "数据不存在", |
| 81 | + //校验数据 key:字段,value:值,type:值类型 |
| 82 | + expected = @Expected(key = "name",value = "123",type = Expected.Type.String)) |
| 83 | + }, |
| 84 | + //开启清理 |
| 85 | + enableClear = true, |
| 86 | + //清理的MongoDB collection |
| 87 | + clearCollectionNames = {"logger"}, |
| 88 | + //清理的db table |
| 89 | + clearTableNames = {"t_demo"} |
| 90 | +) |
| 91 | +public void login_success() { |
| 92 | + try { |
| 93 | + Long id = demoService.login("123"); |
| 94 | + log.info("login - > {}", id); |
| 95 | + Assert.isTrue(id==1 ,"login success ."); |
| 96 | + } catch (UserNameNotFoundException exp) { |
| 97 | + exp.printStackTrace(); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +t_demo.xml |
| 103 | +``` |
| 104 | +<XmlInfo> |
| 105 | + <!--表名称 --> |
| 106 | + <name>t_demo</name> |
| 107 | + <!--插入语句自动创建的 --> |
| 108 | + <initCmd>insert into t_demo(id,name) values(#{id},#{name})</initCmd> |
| 109 | + <!--数据库类型 --> |
| 110 | + <dbType>Mysql</dbType> |
| 111 | + <!--entity所在类 --> |
| 112 | + <className>com.codingapi.cidemo.domain.Demo</className> |
| 113 | + <list> |
| 114 | + <!--数据 --> |
| 115 | + <list> |
| 116 | + <id>1</id> |
| 117 | + <name>123</name> |
| 118 | + </list> |
| 119 | + <list> |
| 120 | + <id>1</id> |
| 121 | + <name>123</name> |
| 122 | + </list> |
| 123 | + </list> |
| 124 | +</XmlInfo> |
| 125 | +
|
| 126 | +``` |
| 127 | + |
| 128 | +mongo.xml |
| 129 | +``` |
| 130 | +<XmlInfo> |
| 131 | + <!--collection名称 --> |
| 132 | + <name>logger</name> |
| 133 | + <!--暂不需要 --> |
| 134 | + <initCmd></initCmd> |
| 135 | + <!--数据库类型 --> |
| 136 | + <dbType>Mongo</dbType> |
| 137 | + <!--entity所在类 --> |
| 138 | + <className>com.codingapi.cidemo.collection.Logger</className> |
| 139 | + <list> |
| 140 | + <!--数据 --> |
| 141 | + <list> |
| 142 | + <id>1</id> |
| 143 | + <time>2</time> |
| 144 | + <info>3</info> |
| 145 | + </list> |
| 146 | + <list> |
| 147 | + <id>1</id> |
| 148 | + <time>2</time> |
| 149 | + <info>3</info> |
| 150 | + </list> |
| 151 | + </list> |
| 152 | +</XmlInfo> |
| 153 | +
|
| 154 | +``` |
| 155 | + |
| 156 | +如何创建 数据模块xml |
| 157 | + |
| 158 | +增加`@XmlBuild` 配置表名称和类型即可 |
| 159 | +``` |
| 160 | +@Data |
| 161 | +@XmlBuild(name = "t_demo",dbType= DBType.Mysql) |
| 162 | +public class Demo extends BaseVO { |
| 163 | +
|
| 164 | + private Long id; |
| 165 | +
|
| 166 | + private String name; |
| 167 | +
|
| 168 | +} |
| 169 | +
|
| 170 | +``` |
| 171 | + |
| 172 | +``` |
| 173 | +#xml创建模式 分为:创建并覆盖、增量、不添加 |
| 174 | +codingapi.test.mode=addition |
| 175 | +#xml数据位置 |
| 176 | +codingapi.test.outPath=${user.dir}/xml |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +## 如何使用? |
| 182 | + |
| 183 | +1、配置maven |
| 184 | + |
| 185 | +``` |
| 186 | +<dependency> |
| 187 | + <groupId>com.codingapi</groupId> |
| 188 | + <artifactId>codingapi-test</artifactId> |
| 189 | + <version>0.0.1</version> |
| 190 | +</dependency> |
| 191 | +``` |
| 192 | + |
| 193 | +2、配置Test类 |
| 194 | + |
| 195 | +```java |
| 196 | +@RunWith(SpringRunner.class) |
| 197 | +@SpringBootTest |
| 198 | +// 单元测试的profile |
| 199 | +@ActiveProfiles("test") |
| 200 | +@Slf4j |
| 201 | +// 增加监听 |
| 202 | +@TestExecutionListeners({JunitMethodListener.class, |
| 203 | + DependencyInjectionTestExecutionListener.class}) |
| 204 | +public class DemoServiceTest { |
| 205 | + |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +3、配置application文件 |
| 210 | + |
| 211 | +``` |
| 212 | +codingapi.test.mode=addition |
| 213 | +codingapi.test.outPath=${user.dir}/xml |
| 214 | +``` |
| 215 | + |
| 216 | + |
| 217 | +4、demo地址 |
| 218 | + |
| 219 | +[https://github.com/1991wangliang/ci-demo](https://github.com/1991wangliang/ci-demo) |
0 commit comments