如果在1.4和更低版本的系统上运行测试文件,我该如何跳过这个测试文件? [英] How do I skip a tests file if it is run on systems with go 1.4 and below?

查看:59
本文介绍了如果在1.4和更低版本的系统上运行测试文件,我该如何跳过这个测试文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以使用获得Go运行时版本, runtime.Version()并进行各种比较。然而,测试文件会导入 golang.org/x/净/ http2 http2 包需要 net / http request.Cancel()



这会导致CI环境中的这些错误导致构建失败:

  ../../../ golang.org/x/net/http2/transport.go:214:req.Cancel undefined(type * http。请求没有字段或方法取消)
../../../golang.org/x/net/http2/transport.go:218:req.Cancel undefined(type * http.Request has no field或方法取消)
../../../golang.org/x/net/http2/transport.go:777:req.Cancel undefined(type * http.Request has no field or method Cancel)

我试着添加 // + build go1.5 到文件顶部,但它不起作用。

是否有限制我可以限制单元测试文件,以便它只在Go 1.5+系统?

解决方案

构建约束是正确的方法。



但请注意,您的错误消息引用了 http2 在1.6版中添加的软件包,所以您至少需要 go1.6 构建约束。



构建约束

  // + build go1.5 

会导致文件要使用 Go 1.5及以后版本进行编译。所以,如果你想让你的测试文件只能用Go 1.6或更高版本编译和运行,那么使用

  // + build go1。 6 

另外不要忘记:

< blockquote>

约束可能出现在任何类型的源文件中(不仅仅是Go),但它们必须在文件顶部附近出现,仅在空行和其他行注释之前。这些规则意味着在Go文件中,构建约束必须出现在package子句之前。



为了区分构建约束和包文档,一系列构建约束必须是然后是一个空行

一个工作示例:

  1 // + build build go1.6 
2
3 package yourpackage


I have a file containing some tests that should be run on Go 1.5+.

I am able to get the Go runtime version using runtime.Version() and doing various comparisons.

However, the test file imports golang.org/x/net/http2. The http2 package requires request.Cancel() from net/http, but that is only available on Go 1.5+.

That causes these errors in my CI environment causing the build to fail:

../../../golang.org/x/net/http2/transport.go:214: req.Cancel undefined (type *http.Request has no field or method Cancel)
../../../golang.org/x/net/http2/transport.go:218: req.Cancel undefined (type *http.Request has no field or method Cancel)
../../../golang.org/x/net/http2/transport.go:777: req.Cancel undefined (type *http.Request has no field or method Cancel)

I tried adding // +build go1.5 to the top of the file, but it didn't work.

Is there anyway I can limit a unit test file so that it is built and tested only on Go 1.5+ systems?

解决方案

The build constraints is the proper way to do it.

But note that your error messages refer to the http2 package which was added in Go 1.6, so you need at least go1.6 build constraint.

The build constraint

// +build go1.5

Will cause the file to be compiled with Go 1.5 and onward. So if you want your test file to only compile and run with Go 1.6 and above, then use

// +build go1.6

Also don't forget that:

Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

A working example:

1  // +build go1.6
2
3  package yourpackage

这篇关于如果在1.4和更低版本的系统上运行测试文件,我该如何跳过这个测试文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆