单元测试(三)spock框架

推荐大家在写单侧的时候使用spock框架, 使用groovy语言进行编写单侧用例的时候, 要比JUnit简单, 而且方便的多, 会节省大家很多的代码.

官网学习地址:http://spockframework.org/spock/docs/1.3/index.html

1. 介绍

我们来看一下官网的介绍,

2. spock 语法

2.1 导入包

import spock.lang.*

2.2 Specification

所有的测试类, 都必须统一继承Specification类

class MyFirstSpecification extends Specification {
  // fields
  // fixture methods
  // feature methods
  // helper methods
}

2.3 Fields(字段, 属性)

def obj = new ClassUnderSpecification()
def coll = new Collaborator()

static final PI = 3.141592654

2.4 Fixture Methods

预先定义的几个固定的函数,与junit或testng中类似,不多解释了

def setupSpec() {}    // runs once -  before the first feature method
def setup() {}        // runs before every feature method
def cleanup() {}      // runs after every feature method
def cleanupSpec() {}  // runs once -  after the last feature method

2.5 Feature Methods(单侧函数)

在写单侧的时候, 我们都需要定义函数, 以下是定义函数的示例:

def "pushing an element on the stack"() {
  // blocks go here
}

2.6 Blocks

每个feture method 又被划分为不同的block, 不同的block处于测试执行的不同阶段, 在各个block 按照不同顺序和规则执行

2.6.1 setup blocks

在这个block中会放置测试函数初始化程序

setup:
def stack = new Stack()
def elem = "push me"

2.6.2 given Blocks

given: 给定一个前提条件

given:
de
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值