在使用 Golang 子进程测试时,如何生成命令行标志的单元测试覆盖率?

huangapple go评论72阅读模式
英文:

How to generate unit test coverage when using command line flags in Golang subprocess testing?

问题

我对大部分代码都有单元测试。但是我无法弄清楚如何为主包中的 main() 函数生成单元测试覆盖率。

主函数非常简单。它基本上是一个 select 块。它读取标志,然后要么调用另一个函数/执行某些操作,要么只是在屏幕上打印帮助信息。然而,如果命令行选项没有正确设置,它将以各种错误代码退出。因此,需要进行子进程测试。

我尝试了子进程测试技术,但修改了代码以包含覆盖率标志:

cmd := exec.Command(os.Args[0], "-test.run=TestMain -test.coverprofile=/vagrant/ucover/coverage2.out")

这是原始代码:https://talks.golang.org/2014/testing.slide#23
上面幻灯片的解释:http://youtu.be/ndmB0bj7eyw?t=47m16s

但它没有生成覆盖率文件。我一直没有弄清楚为什么。它确实为执行测试的主进程生成覆盖率文件,但在子进程中执行的任何代码当然不会被标记为已执行。

我试图尽可能实现代码覆盖率。我不确定是否遗漏了什么,或者是否有更简单的方法来做到这一点。或者是否根本不可能。

非常感谢您的帮助。

谢谢

Amer

英文:

I have unit tests for most of our code. But I cannot figure out how to generate unit tests coverage for certain code in main() in main package.

The main function is pretty simple. It is basically a select block. It reads flags, then either call another function/execute something, or simply print help on screen. However, if commandline options are not set correctly, it will exit with various error codes. Hence, the need for sub-process testing.

I tried sub-process testing technique but modified code so that it include flag for coverage:

cmd := exec.Command(os.Args[0], "-test.run=TestMain -test.coverprofile=/vagrant/ucover/coverage2.out")

Here is original code: https://talks.golang.org/2014/testing.slide#23
Explanation of above slide: http://youtu.be/ndmB0bj7eyw?t=47m16s

But it doesn't generate cover profile. I haven't been able to figure out why not. It does generate cover profile for main process executing tests, but any code executed in sub-process, of course, is not marked as executed.

I try to achieve as much code coverage as possible. I am not sure if I am missing something or if there is an easier way to do this. Or if it is just not possible.

Any help is appreciated.

Thanks

Amer

答案1

得分: 6

我采用了另一种方法,不涉及重构main()函数:请参考此提交

我使用了一个全局(未导出的)变量:

var args []string

然后在main()函数中,除非私有变量args已设置,否则使用os.Args

a := os.Args[1:]
if args != nil {
    a = args
}
flag.CommandLine.Parse(a)

在我的测试中,我可以设置我想要的参数:

args = []string{"-v", "-audit", "_tests/p1/conf/gitolite.conf"}
main()

即使在main()函数上,我仍然实现了100%的代码覆盖率。

英文:

I went with another approach which didn't involve refactoring main(): see this commit:

I use a global (unexported) variable:

var args []string

And then in main(), I use os.Args unless the private var args was set:

a := os.Args[1:]
if args != nil {
    a = args
}
flag.CommandLine.Parse(a)

In my test, I can set the parameters I want:

args = []string{"-v", "-audit", "_tests/p1/conf/gitolite.conf"}
main()

And I still achieve a 100% code coverage, even over main().

答案2

得分: 4

我会将需要测试的逻辑从main()中分离出来:

func main() {
    start(os.Args)
}

func start(args []string) {
    // 旧的main()逻辑
}

这样你就可以对start()进行单元测试,而不会改变os.Args

英文:

I would factor the logic that needs to be tested out of main():

func main() {
	start(os.Args)
}

func start(args []string) {
	// old main() logic
}

This way you can unit-test start() without mutating os.Args.

答案3

得分: 1

使用@VonC的解决方案,结合Go 1.11版本,我发现在每次重新定义标志位时,需要重置flag.CommandLine,以避免出现“标志位重定义”恐慌。

	for _, check := range checks {
		t.Run("flagging " + check.arg, func(t *testing.T) {
			flag.CommandLine = flag.NewFlagSet(cmd, flag.ContinueOnError)
			args = []string{check.arg}
			main()
		})
	}
英文:

Using @VonC solution with Go 1.11, I found I had to reset flag.CommandLine on each test redefining the flags, to avoid a "flag redefined" panic.:

	for _, check := range checks {
		t.Run("flagging " + check.arg, func(t *testing.T) {
			flag.CommandLine = flag.NewFlagSet(cmd, flag.ContinueOnError)
			args = []string{check.arg}
			main()
		})
	}

huangapple
  • 本文由 发表于 2014年9月25日 00:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/26022230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定