针对特定的操作系统或架构进行测试的Go测试文件。

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

Go test file for specific OS or architecture

问题

在Go语言中,文件名具有语义意义。例如:

*_windows.go // 仅在Windows编译时包含
*_unix.go    // 仅在Unix编译时包含
*_386.go     // 仅在386系统编译时包含
*_test.go    // 仅在运行`go test`时包含

然而,我似乎无法使以下内容起作用:

*_windows_test.go // 仅在Windows上运行`go test`时包含
*_test_windows.go // 另一种尝试

在Go语言中是否可能实现这个功能?如果是,该如何实现?

英文:

In Go, filenames have semantic meaning. For example:

*_windows.go // Only include in compilations for Windows
*_unix.go    // Only include in compilations for Unix
*_386.go     // Only include in compilations for 386 systems
*_test.go    // Only include when run with `go test`

However, I can't seem to get the following to work:

*_windows_test.go // Only include when running `go test` on windows
*_test_windows.go // Another attempt

Is this even possible with Go? If so, how?

答案1

得分: 4

只需在测试文件上使用构建约束

// +build windows

构建约束是由空格分隔的选项的逻辑或运算;每个选项由逗号分隔的项的逻辑与运算组成;每个项是一个字母数字单词,或者在前面加上!表示其否定。也就是说,构建约束的格式为:

英文:

Just use a build constraint on the test files.

// +build windows

> A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:

答案2

得分: 3

原来我错了。它确实有效。问题在于我还有一个名为 foo_unix_test.go 的文件,显然 Go 不支持 *_unix.go 语法作为特殊情况。

英文:

Turns out I was wrong. It does work. The issue was that I also had a file called foo_unix_test.go, and apparently Go doesn't support the *_unix.go syntax as a special case.

huangapple
  • 本文由 发表于 2015年2月8日 05:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28387378.html
匿名

发表评论

匿名网友

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

确定