ITPub博客

首页 > 数据库 > 数据库开发技术 > Groovy单元测试框架spock数据驱动Demo

Groovy单元测试框架spock数据驱动Demo

原创 数据库开发技术 作者:FunTester 时间:2019-11-17 10:11:05 0 删除 编辑
★

spock是一款全能型的单元测试框架。

”

上次文章分享了spock框架的基础功能的使用,在此基础上,我根据自己写的Groovy的封装方法、数据驱动以及一些Groovy的高级语法做了一些尝试。发现还是有一点点问题,不知道是不是因为我本身是Java和Groovy混编的项目导致的,不过也都有了解决方案。

分享代码,供各位参考:

  1package com.FunTester.spock.pratice
2
3import com.fun.config.PropertyUtils
4import com.fun.frame.SourceCode
5import org.slf4j.Logger
6import spock.lang.Shared
7import spock.lang.Specification
8
9import static com.fun.config.Constant.EMPTY
10import static com.fun.config.Constant.getLongFile
11import static com.fun.frame.Output.output
12import static com.fun.frame.SourceCode.*
13
14class Test02 extends Specification {
15
16    @Shared
17    def properties = PropertyUtils.getLocalProperties(getLongFile("1"))
18
19    @Shared
20    def cc = Arrays.asList(properties.getArrays("c")).stream().map {x -> Integer.valueOf(x)}.collect() as List
21
22    @Shared
23    def bb = Arrays.asList(properties.getArrays("b")).stream().map {x -> Integer.valueOf(x)}.collect() as List
24
25    @Shared
26    def aa = Arrays.asList(properties.getArrays("a")).stream().map {x -> Integer.valueOf(x)}.collect() as List
27
28    @Shared
29    Logger logger = getLogger(Test02.class.getName())
30
31    def setup() {
32        logger.info("测试方法开始了")
33    }
34
35    def cleanup() {
36        logger.info("测试方法结束了")
37    }
38
39    def setupSpec() {
40        logger.info("测试类[${getClass().getName()}]开始了")
41    }
42
43    def cleanupSpec() {
44        logger.info("测试类[${getClass().getName()}]结束了")
45    }
46
47    def "测试数据驱动Demo"() {
48        given:
49        int c = 0
50
51        expect:
52        10 == a + b + c
53
54        where:
55        a | b
56        1 | 9
57        8 | 2
58    }
59
60    def "测试数据读写完成数据驱动"() {
61        given:
62        def a = 0
63        def arrays = properties.getArrays("id")
64        def s1 = properties.getPropertyInt("size1")
65        def s2 = properties.getPropertyInt("size2")
66        def list = Arrays.asList(arrays).stream().filter {x -> x.length() > 1}.collect() as List
67
68        expect:
69        s1 == arrays.size()
70        s2 == list.size()
71    }
72
73    def "测试自定义对象操作"() {
74        given: "给一个自定义对象"
75        def per = new Per()
76        per.age = 23
77        per.name = "FunTester"
78        def a = per
79
80        expect:
81        a.name == "FunTester"
82
83    }
84
85    def "线程安全测试"() {
86        given: "多线程支持测试,此处线程数改成很大之后效果比较明显"
87        range(2).forEach {new Per().start()}
88        sleep(1000)
89        output(Per.i)
90        expect:
91        4 == Per.i
92    }
93
94    def "测试集合验证使用数据驱动"() {
95        given: "此处写的无法被where使用"
96
97        expect:
98        c * c == a * a + b * b
99
100        where:
101        c << cc
102        b << bb
103        a << aa
104    }
105
106    def "测试数组0..10方式是否可用"() {
107        expect:
108        true == SourceCode.isNumber(x + EMPTY)
109
110        where: "需要用括号,不然会报错"
111        x << (0..5)
112
113    }
114
115    def "测试lambda写法是否可用"() {
116        given:
117        def collect =  range(10).filter {x -> x % 2 == 1}.collect() as List
118
119        expect:
120        collect.size() == 5
121        collect.contains(3)
122        collect.contains(5)
123    }
124
125
126/**
127 * 测试类
128 */

129    class Per extends Thread {
130
131        static int i
132
133        @Override
134        public void run() {
135            i++
136            sleep(100)
137            i++
138        }
139
140        Per() {
141        }
142
143        Per(String name, int age) {
144            this()
145            this.name = name
146            this.age = age
147        }
148
149        String name
150
151        int age
152
153        static def getAll() {
154            i
155        }
156
157    }
158
159}


下次我会针对自己写的工具类和封装的请求对象进行一些spock方面的代码演示,欢迎各位关注。

技术类文章精选

非技术文章精选


原文作者:FunTester;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-07-07

  • 博文量
    1037
  • 访问量
    420769