Groovy單元測試框架spock數據驅動Demo

spock是一款全能型的單元測試框架。java

上次文章分享了spock框架的基礎功能的使用,在此基礎上,我根據本身寫的Groovy的封裝方法、數據驅動以及一些Groovy的高級語法作了一些嘗試。發現仍是有一點點問題,不知道是否是由於我自己是Java和Groovy混編的項目致使的,不過也都有了解決方案。編程

分享代碼,供各位參考:數組

package com.FunTester.spock.pratice

import com.fun.config.PropertyUtils
import com.fun.frame.SourceCode
import org.slf4j.Logger
import spock.lang.Shared
import spock.lang.Specification

import static com.fun.config.Constant.EMPTY
import static com.fun.config.Constant.getLongFile
import static com.fun.frame.Output.output
import static com.fun.frame.SourceCode.*

class Test02 extends Specification {

    @Shared
    def properties = PropertyUtils.getLocalProperties(getLongFile("1"))

    @Shared
    def cc = Arrays.asList(properties.getArrays("c")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    def bb = Arrays.asList(properties.getArrays("b")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    def aa = Arrays.asList(properties.getArrays("a")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    Logger logger = getLogger(Test02.class.getName())

    def setup() {
        logger.info("測試方法開始了")
    }

    def cleanup() {
        logger.info("測試方法結束了")
    }

    def setupSpec() {
        logger.info("測試類[${getClass().getName()}]開始了")
    }

    def cleanupSpec() {
        logger.info("測試類[${getClass().getName()}]結束了")
    }

    def "測試數據驅動Demo"() {
        given:
        int c = 0

        expect:
        10 == a + b + c

        where:
        a | b
        1 | 9
        8 | 2
    }

    def "測試數據讀寫完成數據驅動"() {
        given:
        def a = 0
        def arrays = properties.getArrays("id")
        def s1 = properties.getPropertyInt("size1")
        def s2 = properties.getPropertyInt("size2")
        def list = Arrays.asList(arrays).stream().filter {x -> x.length() > 1}.collect() as List

        expect:
        s1 == arrays.size()
        s2 == list.size()
    }

    def "測試自定義對象操做"() {
        given: "給一個自定義對象"
        def per = new Per()
        per.age = 23
        per.name = "FunTester"
        def a = per

        expect:
        a.name == "FunTester"

    }

    def "線程安全測試"() {
        given: "多線程支持測試,此處線程數改爲很大以後效果比較明顯"
        range(2).forEach {new Per().start()}
        sleep(1000)
        output(Per.i)
        expect:
        4 == Per.i
    }

    def "測試集合驗證使用數據驅動"() {
        given: "此處寫的沒法被where使用"

        expect:
        c * c == a * a + b * b

        where:
        c << cc
        b << bb
        a << aa
    }

    def "測試數組0..10方式是否可用"() {
        expect:
        true == SourceCode.isNumber(x + EMPTY)

        where: "須要用括號,否則會報錯"
        x << (0..5)

    }

    def "測試lambda寫法是否可用"() {
        given:
        def collect =  range(10).filter {x -> x % 2 == 1}.collect() as List

        expect:
        collect.size() == 5
        collect.contains(3)
        collect.contains(5)
    }


/**
 * 測試類
 */
    class Per extends Thread {

        static int i

        @Override
        public void run() {
            i++
            sleep(100)
            i++
        }

        Per() {
        }

        Per(String name, int age) {
            this()
            this.name = name
            this.age = age
        }

        String name

        int age

        static def getAll() {
            i
        }

    }

}

下次我會針對本身寫的工具類和封裝的請求對象進行一些spock方面的代碼演示,歡迎各位關注。安全

技術類文章精選

非技術文章精選

相關文章
相關標籤/搜索