在 go 中构建单个测试
Build single test in go
我的项目中有两个测试,我想构建一个测试,将生成的二进制文件放入容器中,运行它,然后附加调试器。
这可能吗?
package dataplatform
import "testing"
func TestA(t *testing.T) {
// test A
}
func TestRunCommand(t *testing.T) {
// Test B
}
您可以使用 -run <regexp>
将测试限制(过滤)到 运行。因此,例如,如果您只想 运行 测试 TestA()
,您可以这样做:
go test -run TestA
实际上以上将 运行 所有名称包含 TestA
的测试,所以明确地说,它将是:
go test -run ^TestA$
要不 运行 测试而是生成测试二进制文件,您可以使用 -c
选项:
go test -c
这不会 运行 测试,但会编译一个二进制文件,执行时会 运行 测试。
问题是您不能组合这些选项,例如运行宁
go test -c -run TestA
将生成一个二进制文件,执行时将 运行 所有测试。
事实是生成的二进制文件接受与 go test
相同的参数,因此您可以将 -run TestA
传递给生成的二进制文件,但必须在参数前加上 test
:
Each of these flags is also recognized with an optional 'test.' prefix, as in -test.v. When invoking the generated test binary (the result of 'go test -c') directly, however, the prefix is mandatory.
因此,如果生成的测试二进制文件的名称是 my.test
,运行,它就像:
./my.test -test.run TestA
有关更多选项和文档,运行 go help test
,或访问官方文档:
以及相关部分:
我的项目中有两个测试,我想构建一个测试,将生成的二进制文件放入容器中,运行它,然后附加调试器。
这可能吗?
package dataplatform
import "testing"
func TestA(t *testing.T) {
// test A
}
func TestRunCommand(t *testing.T) {
// Test B
}
您可以使用 -run <regexp>
将测试限制(过滤)到 运行。因此,例如,如果您只想 运行 测试 TestA()
,您可以这样做:
go test -run TestA
实际上以上将 运行 所有名称包含 TestA
的测试,所以明确地说,它将是:
go test -run ^TestA$
要不 运行 测试而是生成测试二进制文件,您可以使用 -c
选项:
go test -c
这不会 运行 测试,但会编译一个二进制文件,执行时会 运行 测试。
问题是您不能组合这些选项,例如运行宁
go test -c -run TestA
将生成一个二进制文件,执行时将 运行 所有测试。
事实是生成的二进制文件接受与 go test
相同的参数,因此您可以将 -run TestA
传递给生成的二进制文件,但必须在参数前加上 test
:
Each of these flags is also recognized with an optional 'test.' prefix, as in -test.v. When invoking the generated test binary (the result of 'go test -c') directly, however, the prefix is mandatory.
因此,如果生成的测试二进制文件的名称是 my.test
,运行,它就像:
./my.test -test.run TestA
有关更多选项和文档,运行 go help test
,或访问官方文档:
以及相关部分: