gpt4 book ai didi

go 测试用例未在主包中运行

转载 作者:数据小太阳 更新时间:2023-10-29 03:31:20 26 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个简单的测试以更好地理解 golang 测试,但测试用例似乎没有执行,我预计它会失败。

在我的 main.go 中我有:

package main

import "fmt"

func main() {
fmt.Println("run")
}

func twoSum(nums []int, target int) []int {
lookup := make(map[int]int)
for i, n := range nums {
c := target - n
if j, ok := lookup[c]; ok {
return []int{j, i}
}
lookup[n] = i
}
return []int{}
}

然后在我的 main_test.go 中我有这个:

package main

import (
"reflect"
"testing"
)

var twoSumsCases = []struct{
input []int
target int
expected []int

} {
{
[]int{2,7,11,15},
9,
[]int{0,3},

},
}

func TesttwoSum(t *testing.T) {

for _, tc := range twoSumsCases {
actual := twoSum(tc.input, tc.target)

eq := reflect.DeepEqual(actual, tc.expected)

if eq {
t.Log("expected: ", tc.expected, " actual: ", actual)
} else {
t.Error("expected: ", tc.expected, " actual: ", actual)

}
}
}

然后当我运行 go test -v 时...它告诉我 testing: warning: no tests to run。我以这个为例:https://blog.alexellis.io/golang-writing-unit-tests/...and我想我已经得到了我需要的一切,但不确定为什么测试没有执行。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com