尽管我有一个测试函数,为什么会显示“没有要运行的测试”?

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

Why am I getting "no tests to run" despite having a test function?

问题

Go新手在这里,我编写了一个简单的main_test.go文件来运行一些main.go的测试用例,当我运行go test时,它显示testing: warning: no tests to run
PASS
ok Solution 0.878s

我的main.go:

package main

func normalizePhoneNum(phoneNumber string) string {
	return ""
}

func main() {

}

main_test.go:

package main

import (
	"testing"
)

func testNormalizePhoneNum(t *testing.T) {
	testCase := []struct {
		input  string
		output string
	}{
		{"1234567890", "1234567890"},
		{"123 456 7891", "123 456 7891"},
		{"(123) 456 7892", "(123) 456 7892"},
		{"(123) 456-7893", "(123) 456-7893"},
		{"123-456-7894", "123-456-7894"},
		{"123-456-7890", "123-456-7890"},
		{"1234567892", "1234567892"},
		{"(123)456-7892", "(123)456-7892"},
	}
	for _, tc := range testCase {
		t.Run(tc.input, func(t *testing.T) {
			actual := normalizePhoneNum(tc.input)
			if actual != tc.output {
				t.Errorf("for %s: got %s, expected %s", tc.input, actual, tc.output)
			}
		})
	}
}

有人能告诉我为什么它不运行测试用例吗?

英文:

Go newbie here, I have written a simple main_test.go file to run some test cases for main.go, when I run go test it says testing: warning: no tests to run
PASS
ok Solution 0.878s

my main.go:

package main

func normalizePhoneNum(phoneNumber string) string {
	return ""
}

func main() {

}

main_test.go:

package main

import (
	"testing"
)

func testNormalizePhoneNum(t *testing.T) {
	testCase := []struct {
		input  string
		output string
	}{
		{"1234567890", "1234567890"},
		{"123 456 7891", "123 456 7891"},
		{"(123) 456 7892", "(123) 456 7892"},
		{"(123) 456-7893", "(123) 456-7893"},
		{"123-456-7894", "123-456-7894"},
		{"123-456-7890", "123-456-7890"},
		{"1234567892", "1234567892"},
		{"(123)456-7892", "(123)456-7892"},
	}
	for _, tc := range testCase {
		t.Run(tc.input, func(t *testing.T) {
			actual := normalizePhoneNum(tc.input)
			if actual != tc.output {
				t.Errorf("for %s: got %s, expected %s", tc.input, actual, tc.output)
			}
		})
	}
}

Can anyone please tell, why it's not running the test cases?

答案1

得分: 28

基础!请参阅go test命令的文档

测试函数的命名规则是TestXxx(其中Xxx不以小写字母开头),并且应该具有以下签名:

func TestXxx(t *testing.T) { ... }

请注意,第一个字母必须是大写的T。您必须遵守此命名约定以便测试工具能够正确识别它们。

将您的测试函数重命名为TestNormalizePhoneNum,然后尝试再次运行go test


或者,虽然这很可能不是您想要的,但您可以通过在-run标志中指定其名称(或更一般地,指定与其名称匹配的正则表达式)来强制测试工具运行一个不符合命名约定的“测试函数”:

go test -run=testNormalizePhoneNum
英文:

Elementary! See the documentation for the go test command:

> A test function is one named TestXxx (where Xxx does not start with a lower case letter) and should have the signature,
>
> func TestXxx(t *testing.T) { ... }

Note that the first letter must be an uppercase T. You must respect this naming convention for test functions or the testing tool will simply ignore them.

Rename your test function to TestNormalizePhoneNum and try running go test again.


Alternatively—and although that is most likely not what you want here—you can force the testing tool to run a "test function" that doesn't adhere to the naming convention by specifying its name (or, more generally, a regular expression that its name matches) in the -run flag:

go test -run=testNormalizePhoneNum

huangapple
  • 本文由 发表于 2021年8月29日 18:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68971920.html
匿名

发表评论

匿名网友

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

确定