淺談單元測試

單元測試或是最好的項目文檔。

很早以前在學習使用Java作測試的時候,獲得過一個神祕大佬的幫助,在一塊兒聊過單元測試,基本結論就是:單元測試大機率沒啥鳥用。java

衆所周知,自動化測試相比手動測試一個比較明顯的特色就是見效慢,須要積累必定的時間所產生的的價值才能超過手動測試,這仍是在比較理想的狀況下。某些時候可能永遠也超不過。而單元測試更甚,據大佬和吹牛逼的羣聊中判斷:好的單元測試代碼大概是被測代碼的2-3倍,這種工做量對於開發人員來說是不可接受的。單元測試見效比自動化測試更慢,這一點也是你們的共識,甚至到不了見效的時候就黃了。apache

以前對單元測試進行過一些嘗試,寫過一點文章:編程

近幾日一直在對以前的性能測試框架進行優化,在這個過程當中,我以前利用Groovy單元測試框架spock寫過的兩個性能測試框架的單元用例起到了很是大的幫助,不用再去檢查各個類的實現代碼有沒有忘記修改的,直接運行用例,看結果便可。分享出來,供參考:併發

  • 中間我用了Groovy對象乘法運算符重載的功能。
package com.FunTester.mockito.utils_test

import com.fun.base.constaint.ThreadBase
import com.fun.base.constaint.ThreadLimitTimesCount
import com.fun.base.interfaces.MarkThread
import com.fun.config.HttpClientConstant
import com.fun.frame.SourceCode
import com.fun.frame.excute.Concurrent
import com.fun.frame.httpclient.FanLibrary
import com.fun.frame.thead.HeaderMark
import com.fun.frame.thead.RequestThreadTime
import com.fun.frame.thead.RequestThreadTimes
import org.apache.http.client.methods.HttpGet
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.TEST_ERROR_CODE
import static com.fun.frame.SourceCode.getLogger

class PerformanceTest extends Specification {

    @Shared
    Logger logger = getLogger(this.getClass().getName())

    def setupSpec() {
        logger.info "測試類開始! ${logger.getName()}"
    }

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

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

    def cleanupSpec() {
        logger.info "測試類結束! ${logger.getName()}"
    }

    def "測試併發狀況下記錄響應標記符的"() {
        given:
        HttpGet httpGet = FanLibrary.getHttpGet("https://cn.bing.com/");
        MarkThread mark = new HeaderMark("requestid")
        FanLibrary.getHttpResponse(httpGet);
        HttpClientConstant.MAX_ACCEPT_TIME = -1
        RequestThreadTimes threadTimes = new RequestThreadTimes(httpGet, 2, mark);
        new Concurrent(threadTimes * 2).start();

    }

    def "測試併發狀況下記錄響應標記符的,按照時間壓測"() {
        given:
        HttpGet httpGet = FanLibrary.getHttpGet("https://cn.bing.com/");
        MarkThread mark = new HeaderMark("requestid")
        FanLibrary.getHttpResponse(httpGet);
        HttpClientConstant.MAX_ACCEPT_TIME = -1
        RequestThreadTime threadTimes = new RequestThreadTime(httpGet, 1, mark);
        new Concurrent(threadTimes * 2).start();

    }


    def "測試虛擬類內部類實現"() {
        given:
        def threads = []
        2.times {
            threads << new ThreadLimitTimesCount<Object>(null, 2, new MarkThread() {

                def i = SourceCode.getRandomInt(9) * 100


                @Override
                String mark(ThreadBase threadBase) {
                    return EMPTY + i++
                }

                @Override
                MarkThread clone() {
                    return null
                }
            }) {

                @Override
                protected void doing() throws Exception {
                    sleep(200)
                    logger.info("test method over once .")
                }

            }
        }
        HttpClientConstant.MAX_ACCEPT_TIME = TEST_ERROR_CODE
        new Concurrent(threads).start()

        expect:

        2 == 2

    }
}

這兩個是我練習時候寫的用例,很難講價值有多大,可是當我發現有了一種方式能快速驗證代碼是否能夠正常運行以及快速調試的功能的時候,我以爲都是值的。框架

這些事兒,都是堅持了纔會有效果。dom


  • 鄭重聲明:文章首發於公衆號「FunTester」,禁止第三方(騰訊雲除外)轉載、發表。

技術類文章精選

非技術文章精選

相關文章
相關標籤/搜索