Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取golang测试的分支覆盖率_Go - Fatal编程技术网

获取golang测试的分支覆盖率

获取golang测试的分支覆盖率,go,Go,有人能为我的golang测试提供分支覆盖的建议吗?假设我有一个golang代码,如下所示: package main import ( "fmt" ) func HelloWorld(name string, printv int) string { if name == "tuk" || printv == 1 { fmt.Println("Hello tuk") return "Hello tuk" } else {

有人能为我的golang测试提供分支覆盖的建议吗?假设我有一个golang代码,如下所示:

package main

import (
    "fmt"
)

func HelloWorld(name string, printv int) string {
    if name == "tuk" || printv == 1 {
        fmt.Println("Hello tuk")
        return "Hello tuk"
    } else {
        fmt.Println("Who are you?")
        return "Who are you?"
    }
}
package main

import "testing"

func TestHelloWorld(t *testing.T) {
    r := HelloWorld("tuk", 0)
    if r != "Hello tuk" {
        t.Error("Test Failed")
    }
}
相应的测试文件如下所示:

package main

import (
    "fmt"
)

func HelloWorld(name string, printv int) string {
    if name == "tuk" || printv == 1 {
        fmt.Println("Hello tuk")
        return "Hello tuk"
    } else {
        fmt.Println("Who are you?")
        return "Who are you?"
    }
}
package main

import "testing"

func TestHelloWorld(t *testing.T) {
    r := HelloWorld("tuk", 0)
    if r != "Hello tuk" {
        t.Error("Test Failed")
    }
}
如果我执行下面的测试命令,它只是给我语句覆盖率:

go test -coverprofile=cover.out .
ok      test    0.007s  coverage: 60.0% of statements
有没有办法让我也能得到分支机构的保险

自Go 1.11(2018年11月)起,生成的覆盖范围机器代码。应该可以使用该代码并调整它以覆盖表达式的每个分支

看起来似乎已经做到了这一点,尽管只是针对单个文件,而不是整个软件包(同样,截至2018年11月)

我想看看Go团队是否认为分支机构覆盖是值得的。决定是保持当前代码像现在一样简单,并且这个决定是正确的


最后,添加了我需要的所有功能。

我认为-html报告是查找分支覆盖率的好方法。