美文网首页
gotests 单元测试 快速生成

gotests 单元测试 快速生成

作者: 唐僧取经 | 来源:发表于2018-09-07 13:54 被阅读0次

gotests 单元测试 快速生成

go test单元测试简介

gotests安装

源码地址:https://github.com/cweill/gotests

安装步骤:

    1. 执行下面命令
    go get -u github.com/cweill/gotests
    
  • 2.将下载的gotests目录,配置到PATH环境变量中
    例:
    export PATH=$PATH:$GOBIN:$GOPATH:$MYSQLBIN
    

gotests使用

 -all
        generate tests for all functions and methods
        为所有函数和方法生成go测试
        
  -excl string
  为没有匹配的函数和方法生成go测试
        regexp. generate tests for functions and methods that don't match. Takes precedence over -only, -exported, and -all
        
        
  -exported
  
  为导出的函数和方法生成go测试。
        generate tests for exported functions and methods. Takes precedence over -only and -all
        
  -i    
  在错误消息中打印测试输入
  print test inputs in error messages
  
  -nosubtests
  禁用子测试生成。仅适用于Go 1.7+
        disable generating tests using the Go 1.7 subtests feature
        
        
  -only string
  正则表达式。生成仅匹配的函数和方法的测试。优先于-all
        regexp. generate tests for functions and methods that match only. Takes precedence over -all
  -template_dir string
   可选的。包含自定义测试代码模板的目录的路径
        optional. Path to a directory containing custom test code templates
  -w    
  将输出写入(测试)文件而不是stdout
  
  write output to (test) files instead of stdout


常用操作

1.进入对应的要被生成单元测试的目录
2.在该目录下执行命令

3.生成指定函数的单元测试,输出到命令行,然后复制粘贴到目标处。


gotests -only "函数名称" 文件名称.go

4.生成全部测试函数


gotests -all 文件名称.go

相关文章

网友评论

      本文标题:gotests 单元测试 快速生成

      本文链接:https://www.haomeiwen.com/subject/uiyqgftx.html