gotests 单元测试 快速生成

2018-09-07  本文已影响0人  唐僧取经

gotests 单元测试 快速生成

go test单元测试简介

gotests安装

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

安装步骤:

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

上一篇 下一篇

猜你喜欢

热点阅读