FunTester 浅谈单元测试

FunTester · 2020年04月01日 · Modified By Admin 026 · 572 次阅读

单元测试或是最好的项目文档。

很早之前在学习使用 Java 做测试的时候,得到过一个神秘大佬的帮助,在一起聊过单元测试,基本结论就是:单元测试大概率没啥鸟用。

众所周知,自动化测试相比手动测试一个比较明显的特点就是见效慢,需要积累一定的时间所产生的的价值才能超过手动测试,这还是在比较理想的情况下。某些时候可能永远也超不过。而单元测试更甚,据大佬和吹牛逼的群聊中判断:好的单元测试代码大概是被测代码的 2-3 倍,这种工作量对于开发人员来讲是不可接受的。单元测试见效比自动化测试更慢,这一点也是大家的共识,甚至到不了见效的时候就黄了。

之前对单元测试进行过一些尝试,写过一点文章:

近几日一直在对之前的性能测试框架进行优化,在这个过程中,我之前利用 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

    }
}

这两个是我练习时候写的用例,很难讲价值有多大,但是当我发现有了一种方式能快速验证代码是否可以正常运行以及快速调试的功能的时候,我觉得都是值的。

这些事儿,都是坚持了才会有效果。


  • 郑重声明:文章首发于公众号 “FunTester”,禁止第三方(腾讯云除外)转载、发表。

技术类文章精选

非技术文章精选

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册