前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Groovy单元测试框架spock数据驱动Demo

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

作者头像
FunTester
发布2019-11-18 12:57:28
4180
发布2019-11-18 12:57:28
举报
文章被收录于专栏:FunTesterFunTester

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

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

分享代码,供各位参考:

代码语言:javascript
复制
  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方面的代码演示。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
手游安全测试
手游安全测试(Security Radar,SR)为企业提供私密的安全测试服务,通过主动挖掘游戏业务安全漏洞(如钻石盗刷、服务器宕机、无敌秒杀等40多种漏洞),提前暴露游戏潜在安全风险,提供解决方案及时修复,最大程度降低事后外挂危害与外挂打击成本。该服务为腾讯游戏开放的手游安全漏洞挖掘技术,杜绝游戏外挂损失。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档