使用去建立,但我也看到了 - 测试标志 [英] Using go build but I also see the -test flags

查看:150
本文介绍了使用去建立,但我也看到了 - 测试标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 main.go mypkg / ... go 。我使用 go build -o main main.go go install<包含main.go> 的pkg,以及哪些有一些我需要的标志。但我也看到了测试标志。这是为什么发生?我缺少什么?

  ./main的用法:
-docker字符串
Docker API Path,默认为本地(默认为unix:///var/run/docker.sock)
-httptest.serve字符串
如果非空,则httptest.NewServer服务于此地址并阻止
-port int
要侦听的默认端口(默认值为8000)
-test.bench字符串
每个路径组件的正则表达式选择要运行的基准测试
-test.benchmem
为基准打印内存分配
-test.benchtime持续时间
每个基准测试的大概运行时间(默认1秒)
-test.blockprofile字符串
将goroutine阻止配置文件写入执行后的命名文件
-test.blockprofilerate int
如果> = 0,调用runtime.SetBlockProfileRate()(默认1)

dockerPath和port是我的标志,但正如你所看到的,其他标志不是我的标志。

解决方案

很可能您使用的是默认标志集( flag.FlagSet )的 标志 包。并注意你可能不是唯一使用它的人。如果您导入其他包,他们也可能会注册标志,这些标志将像您自己的标志(您注册的标志)一样进行处理。

请参阅此简单示例:

 进口(
标志
_测试


$ func main(){
flag.Int(port,80,port to use)
flag.Parse()
}

这个应用程序注册了一个 port 标志,没有别的。但它也会导入 测试 包,其中的注册表很多标志。



使用 -h 命令行参数运行它,输出为:

  -port int $ b $使用的端口(默认80)
-test.bench字符串
正则表达式每个路径组件选择基准运行
-test.benchmem
为基准测试打印内存分配
-test.benchtime持续时间
每个基准测试的大致运行时间(默认1秒)
-test.blockprofile字符串
执行后向指定文件写入goroutine阻止配置文件
-test.blockprofilerate int
如果> = 0,调用runtime.SetBlockProfileRate()(默认值为1 )
-test.count n
运行测试和基准测试n次(默认1)
-test.coverprofile字符串
在执行后将覆盖配置文件写入指定文件
-test.cpu字符串
用于每个测试的CPU数量的逗号分隔列表
-test.cpuprofile字符串
在执行过程中将cpu配置文件写入指定文件
-test.memprofile string
在执行后为指定文件写入内存配置文件
-test.memprofilerate int
如果> = 0,则设置runtime.MemProfileRate
- test.outputdir字符串
用于写入配置文件的目录
-test.parallel int
最大测试并行度(默认值4)
-test.run字符串
正则表达式选择测试和示例以运行
-test.short
运行较小的测试套件以节省时间
-test.timeout持续时间
如果为正值,则为所有测试设置一个总计时间限制
-test.trace字符串
在执行后将执行跟踪写入指定文件
-test.v
verbose:打印附加输出
退出状态2

如果您不希望您的标志与其他包的标志混合,请通过调用 flag.FlagSet =https://golang.org/pkg/flag/#NewFlagSet =noreferrer> flag.NewFlagSet() ,当然当然您必须使用它的方法而不是标记包的顶层函数。


I have a main.go and mypkg/...go. I use go build -o main main.go or go install <pkg that has main.go> and which has some flags I require. But I also see the test flags. Why is this happening? What am I missing?

Usage of ./main:
  -docker string
        Docker API Path, defaults to local (default "unix:///var/run/docker.sock")
  -httptest.serve string
        if non-empty, httptest.NewServer serves on this address and blocks
  -port int
        The default port to listen (default 8000)
  -test.bench string
        regular expression per path component to select benchmarks to run
  -test.benchmem
        print memory allocations for benchmarks
  -test.benchtime duration
        approximate run time for each benchmark (default 1s)
  -test.blockprofile string
        write a goroutine blocking profile to the named file after execution
  -test.blockprofilerate int
        if >= 0, calls runtime.SetBlockProfileRate() (default 1)

dockerPath and port are my flags, but as you can see the others are not my flags.

解决方案

Most likely you're using the default flag set (flag.FlagSet) of the flag package. And note that you may not be the only one using it. If you import other packages, they might also register flags, which will be processed just like your own flags (flags you registered).

See this simple example:

import (
    "flag"
    _ "testing"
)

func main() {
    flag.Int("port", 80, "port to use")
    flag.Parse()
}

This app registers a port flag, and nothing else. But it also imports the testing package which registers a lot of flags.

Running it with the -h command line argument, the output is:

  -port int
        port to use (default 80)
  -test.bench string
        regular expression per path component to select benchmarks to run
  -test.benchmem
        print memory allocations for benchmarks
  -test.benchtime duration
        approximate run time for each benchmark (default 1s)
  -test.blockprofile string
        write a goroutine blocking profile to the named file after execution
  -test.blockprofilerate int
        if >= 0, calls runtime.SetBlockProfileRate() (default 1)
  -test.count n
        run tests and benchmarks n times (default 1)
  -test.coverprofile string
        write a coverage profile to the named file after execution
  -test.cpu string
        comma-separated list of number of CPUs to use for each test
  -test.cpuprofile string
        write a cpu profile to the named file during execution
  -test.memprofile string
        write a memory profile to the named file after execution
  -test.memprofilerate int
        if >=0, sets runtime.MemProfileRate
  -test.outputdir string
        directory in which to write profiles
  -test.parallel int
        maximum test parallelism (default 4)
  -test.run string
        regular expression to select tests and examples to run
  -test.short
        run smaller test suite to save time
  -test.timeout duration
        if positive, sets an aggregate time limit for all tests
  -test.trace string
        write an execution trace to the named file after execution
  -test.v
        verbose: print additional output
exit status 2

If you don't want your flags to be mixed with flags of other packages, create and use your own flag.FlagSet by calling flag.NewFlagSet(), but then of course you have to use its methods instead of the top-level functions of the flag package.

这篇关于使用去建立,但我也看到了 - 测试标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆