我应该使用哪种方法来测试 golang 中的 func main()?
Which methodology i should use to test func main() in golang?
我在 main.go 中有一个函数 main()
可以完成这项工作,所有其他函数都在它下面(我没有在此处包括它们)。因此,当我为 main
中包含的所有 funcs
编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖 main
函数中的代码。
我知道测试库中有一个 TestMain
func 可以完成这项工作,但我不知道如何让它工作,以便测试覆盖 func main()
。
下面是我的 main()
函数,它没有被测试覆盖...
func main() {
c, err := getConfig()
if err != nil {
log.Fatal(err)
}
slideshows, err := getSlideshows(c)
if err != nil {
log.Fatal(err)
}
displaySlideshows(slideshows)
}
此外,我在互联网上没有找到太多关于它的信息,所以,如果这是一个愚蠢的问题,请向我解释为什么这是一个愚蠢的问题以及我应该在哪里寻找解决方案!
我将不胜感激任何帮助!
您可以查看 Filippo Valsorda 的“Go coverage with external tests”:
We create a dummy test that executes main()
, we put it behind a build tag, compile a binary with go test -c -cover
and then run only that test instead of running the regular binary.
Here's what the rrdns_test.go
file looks like:
注意构建标签和 package
之间的空行:
// +build testrunmain
package main
import "testing"
func TestRunMain(t *testing.T) {
main()
}
We compile the binary like this:
$ go test -coverpkg="rrdns/..." -c -tags testrunmain rrdns
And then when we want to collect coverage information, we execute this instead of ./rrdns
(and run our test battery as usual):
$ ./rrdns.test -test.run "^TestRunMain$" -test.coverprofile=system.out
You must return from main()
cleanly for the profile to be written to disk; in RRDNS
we do that by catching SIGINT
. You can still use command line arguments and standard input normally, just note that you will get two lines of extra output from the test framework.
这类似于 提出:
func main() {
os.Exit(doFunc());
}
我在 main.go 中有一个函数 main()
可以完成这项工作,所有其他函数都在它下面(我没有在此处包括它们)。因此,当我为 main
中包含的所有 funcs
编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖 main
函数中的代码。
我知道测试库中有一个 TestMain
func 可以完成这项工作,但我不知道如何让它工作,以便测试覆盖 func main()
。
下面是我的 main()
函数,它没有被测试覆盖...
func main() {
c, err := getConfig()
if err != nil {
log.Fatal(err)
}
slideshows, err := getSlideshows(c)
if err != nil {
log.Fatal(err)
}
displaySlideshows(slideshows)
}
此外,我在互联网上没有找到太多关于它的信息,所以,如果这是一个愚蠢的问题,请向我解释为什么这是一个愚蠢的问题以及我应该在哪里寻找解决方案!
我将不胜感激任何帮助!
您可以查看 Filippo Valsorda 的“Go coverage with external tests”:
We create a dummy test that executes
main()
, we put it behind a build tag, compile a binary withgo test -c -cover
and then run only that test instead of running the regular binary.Here's what the
rrdns_test.go
file looks like:
注意构建标签和 package
之间的空行:
// +build testrunmain package main import "testing" func TestRunMain(t *testing.T) { main() }
We compile the binary like this:
$ go test -coverpkg="rrdns/..." -c -tags testrunmain rrdns
And then when we want to collect coverage information, we execute this instead of
./rrdns
(and run our test battery as usual):$ ./rrdns.test -test.run "^TestRunMain$" -test.coverprofile=system.out
You must return from
main()
cleanly for the profile to be written to disk; inRRDNS
we do that by catchingSIGINT
. You can still use command line arguments and standard input normally, just note that you will get two lines of extra output from the test framework.
这类似于
func main() { os.Exit(doFunc()); }