说说测试数据哪些事儿

原创
2019/09/12 11:15
阅读数 745

很久没有写博客了。忙,或许是个理由,或许也是个借口,后面准备再写一阵子,一来和基友们交流学习,二来也可以支持 @红薯 兄的工作。

最近在自己团队搞的一个项目中,让开发给数据表中生成一些数据,来进行功能测试或演示,结果程序员哥哥写出来的是这个样子

这特么自己看着也难受,如果给客户演示,客户看了后指定拂袖而去,当然,如果要求程序员哥哥一条条的造数据,成本和时间也都是个问题。百度谷歌一下,感觉要不是外国东东,有的和我们的系统不太好结合,国内也有一些也不太成体系(上面是例行找理由,重点是引出下面一句),所以就准备自己搞一发,当然,以我的判断,搞这么个东东也就半天时间而已(实际化费时间也还真的就半天时间,其中半个小时搞定idea 2019.3,大家都懂的)。

好的,闲话少说,直接上结果。

首先定义接口

package org.tinygroup.datagenerator;
public interface DataGenerator<T,Config> {

    T generate(Config config);

}

接口很简单,就是一个方法,T代表生成数据的类型,Config是需要的参数,下面是Config的源码。

package org.tinygroup.datagenerator;

public class Config {
    private boolean nullable = true;
    private int nullableRate = 10;

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public int getNullableRate() {
        return nullableRate;
    }

    public void setNullableRate(int nullableRate) {
        this.nullableRate = nullableRate;
    }
}

这个类里面,自带两个属性,一个是是否可为空,一个是空值所占比例,最大取值100,最小取值0。

只有可为空的时候,空值占比才生效。

考虑到是否为空及空值占比很多地方都会使用,因此写一个抽象类,把这块统一实现掉

package org.tinygroup.datagenerator;

public abstract class AbstractDataGenerator<T, C extends Config> implements DataGenerator<T, C> {
    protected abstract T genData(C config);

    public T generate(C config) {
        if (config.isNullable()) {
            if (DataGeneratorUtils.RandomDouble(0, 100) <= config.getNullableRate()) {
                return null;
            }
        }
        return genData(config);
    }
}

主要逻辑就是实现了,如果判断是允许为空,那么判断是不是在空值比例内,如果是呢就返回空值,否则就返回产生的数据,这里注意的一点是,在这个抽象类中新增加一个抽象方法genData,留给子类进行实现。

数据生成实现类及其效果

字符串生成

支持大小写字符和数字有其中1种或多种任意组合,支持定长和不定长

Uo1orw4hXofWJS
LtYB1RtDvsl5
n5Su39ub3mhaS
5S6NXpJJ8dhBMD8ptT
jlwA2gRvCOuotPjPz
GIhWaUK5MRlSyt2
YYqjzZ63pFCp1vgy7p
04oh5T5us6Rj
m0q4IL8gHY5HMtGWjI
g4MIaqQlO8l8H
HvOaUdz3Q0
KaT0tO3lT3HQ
null
oUl9YwIi4uV

车牌号码

沪B.56415
冀I.73248
沪D.70022
null
晋H.07685
沪G.83469
京H.11601
沪G.71508
京B.41630
浙H.80067
沪H.98580
晋B.91115
null
浙B.18406

中文段落内容

创中个民的有华造族勇族的族性族因勤敢族族的是勤民常族是个也基勇民基,中。因一。勤个勇个基劳族个敢的一性劳民勇常敢是也非也是,勤是勤族性因常族民民也民造族。民一敢基族性民一是一创族常劳族的常敢。是创常中民非非华创有。一敢常个勇劳创个勤创造个性劳也一是性民常。常一勤中一一勤基造一族族基是个民因的民创非民勇创非是民族因也造劳民个一。个族族民个敢个常族创勤的民民一民是的一族有个一非,造的基个个有勇族是劳民常族,族常勤因的民是是基民一创个个是是的勇,勇的中造基勇性因。民创族。非华一民民造勇是性个民的个有,勤敢民
个勤敢的有基因民的族也劳的民勇民中民劳劳的是民族民造个性个有一因华,一华基创的族族造基劳一性的因创中劳有劳是族基一是民基勇有民。勇敢勤中的创中,是性的的一勤的民劳造民民性敢基个,民也非民族常民一民性是,。性民敢民族也非个族性。族劳华勇个民勤是的民族一造个一民一一的族,中勤创,,,一民,是是的族一一创也,也族中个个的劳个的一个个个非族创是族也性敢中勇是族民民勤的的是敢一也的。华勤族常族
创非有勤民创的是一中民勇是个个是民基。个创敢华是民个民的基,,民族族民造中是。性常族。常勤族是是造非有,劳一勇也族民也民性创的基造华,个创敢因创也族也华个因勤劳也。,非也劳劳民勤是民创一造敢族华性华勇造的个劳勇族个基基也的造个的勇非是族民劳华有族因族。非个是个华基有是,敢造劳华民非的是中族有是个民一勇劳民是因有是勇的敢勤勤勤华民民劳是性勇有民是因,是一一族个。个敢族。一族基创非勇敢创,个的,个个是创性的一民有是的是造的是族常创造的是创的一因是造也个民劳基族非个常劳也的是。一
创个,华华民是一勇有敢,民是有民华基一个性一个创一有一也因因性族个民中性勇创性基个民民性性。个一创华勤民也非族民有个劳常性,民个非创中。非族敢非是民常是个性个有,族常的是一族族造。中敢因常一民的是民族族敢族一一勇的中基非的。中。民勇创有敢的性民性族一是因族是勇性造是有族性是民。劳族一造民民民中勤,基民。民,也有,劳基族勇有有也勇一敢敢个个,族敢,常族基族的族一的族的勇民劳一的民是勤个创基常创劳劳的常基族族。民创
民民劳勇是也的是有。的民族。一一非劳族中是基华勤的基的是一民族的个性创族民是的劳的,也个勇一勤创一敢的华个也基是创是劳民性,劳敢的民。勇,族个华族非华,勇个个因基个民勇的民常也敢创族族勤劳中民敢劳创一非是是性是劳民族民是非。是性中劳劳创个造中。也,是敢。的的勇有。勇造是华常民常中常是是是,族因个造一民非民有基有是是个因一敢中非基是是个劳也劳勇因基一基,创劳基个,是勇是。造常

中文姓名

张艳红
张建国
赵艳红
赵丹
赵猛
李建国
李强
王建国
王猛
张猛
张娜
张强
李娜
李丹
李猛
李猛
赵欣
李娜
赵丽
null
null
王猛

哈哈,上面的名字重名的有一些,是因为我的数据填的比较少,数据多了生产的就更丰富了。

银行卡号码

088292689106266
5748221840993433930
1302311096358133
0035913052725096307
8338814152786584351
9116624206752636397
4424016750434185647
9300202916067778106
6978375534566423243
5359868715506064
7849535724879525
7750647069235182403
null
9560344391113406

这里的银行卡号只是生成的比较像而已,没有任何数据校验,可以指定生成的多种长度。

日期生成

19931001
19950911
19981224
19921224
19981120
20000102
19890828
null
19930809
19950716

Email地址

bvn@zqi.com
null
dmac@gyqskzu.io
fjakhai@null.cn
iyfwn@bfpfxxy.org
xxa@klll.io
ovrwpz@yurz.cc
null
nstibu@null.io
ulzt@eun.com
null@flxipl.io
urp@kaxh.cn
null@tmcu.cn
zqnuafk@null.com

英文描述

most to . Lauren swim fact community to , essay . coach and on others a in disqualification Langford fact Saturday the Saturday . the coach in the , point the disqualification school nonwhite on others is a , that coach Anchorage controversy essay was the . this high , community quickly the on community than the others most coach nonwhite than than neighboring Medium a , that quickly . swim was coach first to curvier on make the on school point Anchorage coach the the neighboring community high stirred a a that with with in team school nonwhite that others swimmer this fact . disqualification . the some neighboring neighboring on nonwhite the most curvier the , Saturday to some than on . point . curvier neighboring neighboring and essay Saturday curvier swimmer pointing with team school Medium fact the , with stirred Medium fact this than the , coach is neighboring Anchorage first controversy point on at most in the Saturday than the swim the community and Lauren neighboring a is this controversy at Saturday neighboring that was on and community make and that and , essay community quickly first community , Medium high Lauren neighboring community in some that quickly essay this high Medium school make . neighboring essay , . community with first make on this The The stirred 
, Medium point . disqualification on high pointing coach point than most team in this team pointing community the high is Saturday the a curvier to disqualification on that school coach others in is high the quickly is Lauren was school stirred disqualification to , at point at that on nonwhite the coach neighboring neighboring swimmer this , some this most first Lauren with swimmer in and community nonwhite . in with Langford essay controversy The a , fact some Anchorage disqualification the to Anchorage Saturday and school at the fact . team this , a at and than point swimmer coach controversy in some others team at in Langford Anchorage make essay that , than on the the make disqualification most the is , a high to the coach the disqualification with in Saturday some the and essay pointing . the controversy , nonwhite was school first The team to the others coach , Medium community the in than pointing Anchorage make the this the coach pointing in team 

英文名字

Jack Wilson
Jackson Miller
Paul Wilson
Jack Brown
Jane Williams
Paul Brown
Jane Davis
white Davis
Jane Jones
Jane Jones
Jane Smith

字典库填的比较少,后面增加字典库即可

选项生成

[男]
[女, 男]
[男]
[男]
[女, 男]
[男]
[女, 男]
[男]
[男]
[男, 女]

支持单选和复选

身份证号码

91302199401199121
10081519880515715X
250404199702045834
null
null
16012519890502790X
071214199909261301
160306199611019215
311119199412043085
null

自增长

1
3
5
7
9
11
13
15
17
19
21
23

支持步长

手机号码

13787529523
13205016836
13287756816
13715666728
null
13962255398
13948510641
13850891053
13905492728
13620358453
13790071472
null
13653052971

指定前后缀

A000001B
A000002B
A000003B
A000004B
A000005B
A000006B
A000007B
A000008B
A000009B
A000010B

支持有序和无序两种,上面是有序,无序如下

A950696B
A667407B
A380925B
A005574B
A004633B
A343517B
A048256B
A524505B
null
A060719B
A980299B

邮编

139285
136535
136044
132540
138984
139835
139932
null

当然,里面还有一些,比如整数、浮点数、双精度数等等,比较普通就不一一列举了。还有就是支持从数据中取值域列表,因此也比较好的解决了主从表关联的问题。

总结

由于采用了扩展机制,每种生成规则也都有自己的参数可以提供,因此可非常好的支持各种业务规则数据的生成,目前已经非常好的满足了测试数据和演示数据的生成,数据的有效性和可读性有了非常好的提升,极大的满足了我又要看起来漂亮,又要成本低廉的目标。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部