第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 Spock 框架編寫(xiě)多個(gè)單元測(cè)試?

如何使用 Spock 框架編寫(xiě)多個(gè)單元測(cè)試?

繁華開(kāi)滿天機(jī) 2021-12-10 16:56:56
這個(gè)測(cè)試的目標(biāo)是取一個(gè)整數(shù)數(shù)組,找到最大值,并計(jì)算該最大值出現(xiàn)的頻率。我將如何更改此代碼以進(jìn)行多次測(cè)試。另外,我想知道這是否是測(cè)試此問(wèn)題的正確方法。我是 TDD 的新手,目前正在練習(xí)編寫(xiě)測(cè)試以解決易于解決的練習(xí)問(wèn)題。謝謝!import spock.lang.Specificationclass BirthdayCandlesTest extends Specification {    def "GetNumberOfMaxHeightCandles"() {        given: "A BirthdayCandles object"        int[] test = [1,1,1,3,3,3,3]        def candles = new BirthdayCandles(test)        when: "I call the max number height method"        def result = candles.getNumberOfMaxHeightCandles()        then: "I should get the frequency count of the max number in the integer array"        result == 4    }}
查看完整描述

2 回答

?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

正如 John Camerin 所說(shuō),您可能正在尋找spock 中的數(shù)據(jù)驅(qū)動(dòng)測(cè)試。


我會(huì)提供一個(gè)稍微不同的答案:


def "GetNumberOfMaxHeightCandles"() {

    given: "A BirthdayCandles object"

    def candles = new BirthdayCandles(testInput)


    when: "I call the max number height method"

    def actual = candles.getNumberOfMaxHeightCandles()


    then: "I should get the frequency count of the max number in the integer array"

    actual == expectedResult


    where:

    testInput                  |     expectedResult

    [1,1,1,3,3,3,3]  as int [] |     4

    [1,1,1,3,3,3,4]  as int [] |     1

}

幾個(gè)觀察:


請(qǐng)注意,我沒(méi)有在這里使用字符串插值(沒(méi)有“$result”和“$test”)


請(qǐng)注意as int[]where 塊中的 。它的替代方案是def candles = new BirthdayCandles(testInput as int [])


查看完整回答
反對(duì) 回復(fù) 2021-12-10
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以添加一個(gè) where: 塊,其中包含一個(gè)值表,其中第一行是變量名,可以在測(cè)試的其余部分使用。例如


def "GetNumberOfMaxHeightCandles"() {

        given: "A BirthdayCandles object"

        def candles = new BirthdayCandles("$test")


        when: "I call the max number height method"

        def result = candles.getNumberOfMaxHeightCandles()


        then: "I should get the frequency count of the max number in the integer array"

        result == "$result"


        where:

        test             |     result

        [1,1,1,3,3,3,3]  |     4

        [1,1,1,3,3,3,4]  |     1

}

只需添加行即可添加測(cè)試變體。


查看完整回答
反對(duì) 回復(fù) 2021-12-10
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)