分享一个一直在用的golang单测小脚本

[本文出自天外归云的博客园]

一个名为exec_test.sh脚本,内容如下:

# 该脚本需要cd到测试文件所在目录下执行
method=$1
# 不传入method参数则运行全部测试
if [ ! -n "$1" ];then
    # 生成测试覆盖率文件
    go test -v -coverprofile=test.out -gcflags=all=-l
    # 生成测试覆盖率html报告
    go tool cover -html=test.out -o test.html
    exit 1
fi
# 传入method参数则测试指定方法
fileList=`ls`
cmd="go test -v"
for file in $fileList
do
    if echo "$file" | grep -q -E '\.go$';then
        cmd="${cmd} $file"
    fi
done
cmd="$cmd -test.run $method -gcflags=all=-l"
$cmd

执行测试的话,就是在待测文件所在目录下调用这个脚本,然后把方法名传进去就可以了,不传方法名执行所有目录下的测试:

sh exec_test.sh 待测方法名
sh exec_test.sh

很好用

 

posted @ 2021-09-24 15:26  天外归云  阅读(195)  评论(0编辑  收藏  举报