Spock入门 - 快速开始&基础语法

环境搭建

  1. 配置pom.xml依赖
<dependencies>
    <!-- 若本机安装了groovy,无需此依赖 -->
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.4.15</version>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.2-groovy-2.4</version>
        <scope>test</scope>
    </dependency>
    <!-- spring spock -->
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-spring</artifactId>
        <version>1.2-groovy-2.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. hello, world
import spock.lang.Specification

class CalculateSpec extends Specification {
    // 初始化
    def setupSpec() {
        calculateService = new CalculateService()
        println ">>>>>>   setupSpec"
    }
    def setup() {
        println ">>>>>>   setup"
    }
    def cleanup() {
        println ">>>>>>   cleanup"
    }
    def cleanupSpec() {
        println ">>>>>>   cleanupSpec"
    }

    def "spock test hello, world"() {
        given:
        def a = 1
        def b = 2

        expect:
        a < b

        println "spock test hello, world TEST FINISHED..."
    }
}

基本语法

  1. 基本的标签组合(搭配)
  • given … expect …
  • given … when … then …
  • when … then …
  • given … expect … where …
  • expect … where …
  • expect

当然given后还可以继续跟and,用于区分不同业务的赋值操作

  1. with() 和 verifyAll()
    with()用于验证对象实例
        with(p) {
            name == "yawn"
            age < 20
            b

verifyAll()用于验证多个断言

expect:
verifyAll {
    a == b
    a == b + 1
    a == b + 2
}

为什么需要verifyAll(),不用有何区别? API文档解释如下:Using verifyAll to assert multiple
expectations together. Normal expectations fail the test on the first failed assertions. Sometimes it is helpful to collect these failures
before failing the test to have more information, this behavior is also known as soft assertions.

  1. 测试方法执行的次数
N * function(x)
  1. 对异常的测试
    thrown()方法:测试的结果需要抛出某个异常时使用,抛出的异常是定义的instance,测试通过
    notThrown()方法:测试的结果不能抛出某个异常时使用,不抛出定义的异常(instance),测试通过

  2. HamcrestMatcher

HamcrestSupport.expect(b, HamcrestMatchers.closeTo(10000, 200))

b的值在10000±200范围内

  1. Stub & Mock
    区别:
    Stub:假设需要测试A类中的方法,但是B、C类目前还没办法调用,或者还没开发完成,那么可以使用Stub来模拟B、C类,即让其返回一个预设的值,方便测试A方法。
    Mock:假设需要测试A类中方法,但是B、C类目前还没办法调用,或者还没开发完成,那么可以使用Mock来模拟B、C类,检查B、C类的调用或者状态的改变。
    在这里插入图片描述
    总结起来,也就是:
  • Stub:用于提供测试的条件
  • Mock:用于检测结果
  1. 其它注解
  • @Ignore
    忽略测试方法
  • @IgnoreRest
    忽略其他测试方法
  • @Unroll
    展开:数据驱动测试中,展开所有的测试结果,分别显示每个测试用例的测试情况

例如当使用数据管道时,可以展开看到每次数据的测试情况。

  • @Timeout
    超时则测试失败
  • @FailsWith(ArithmeticException.class)
    • 记录已经知道的 bug
    • 标记让方法执行失败的测试用例
  1. groovy中使用groovy.sql.Sql访问数据库
    例如,where里面的数据管道来源于db

  2. 数据驱动测试

  • 数据管道
    a的取值分别如下:
where:
a << [1, 2, 3]
b << [2, 3, 4]
c << [3, 4, 5]

打桩Stub用">>",变量赋值def用">>",数据管道赋值用"<<"

  • 数据表(Groovy本身不支持,数据管道的语法糖)
    a | b || c
    1 2 3
    2 3 4
    3 4 5
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值