运行一个只适用于某些版本的 Golang 测试。

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

Running a test in golang that only works for some versions

问题

我这里有一个库(https://github.com/turtlemonvh/altscanner),其中包含一个测试,比较了自定义扫描器与bufio.Scanner的功能。特别是,我正在比较我的方法与 Buffer 方法,该方法直到go1.6才添加

我的实际代码适用于go1.4及更高版本,但我想包含这个测试(我还想添加一个基准测试),使用bufio.Scanner对象的Buffer函数。

如何在允许代码在go1.4和1.5上运行的同时,包含使用go1.6+功能的这些测试呢?

我想答案是使用构建标志,只有在明确请求时才触发执行这些测试(并且我可以通过travis环境变量在我的CI流水线中访问go版本)。我也可以在这里滥用短标志

有没有更简洁的方法?

英文:

I have a library here (https://github.com/turtlemonvh/altscanner) that includes a test comparing functionality of a custom scanner to bufio.Scanner. In particular, I am comparing my approach to the Buffer method which wasn't added until go1.6.

My actual code works with versions of go back to 1.4, but I wanted to include this test (and I'd like to add a benchmark as well) that uses the Buffer function of the bufio.Scanner object.

How can I include these tests that use features of go1.6+ while still allowing code to run for go1.4 and 1.5?

I imagine the answer is using a build flag to trigger the execution of these tests only if explicitly requested (and I do have access to the go version in my CI pipeline via a travis environment variable). I could also abuse the short flag here.

Is there a cleaner approach?

答案1

得分: 1

在发布这个问题几分钟后,我想起了构建约束。Go语言内置了一个处理这种情况的约束条件,即"go的版本必须>= X"。

将该测试移动到单独的文件,并在顶部添加//+build go1.6,问题就解决了。

英文:

A few minutes after posting this I remembered about build constraints. Go has a built in constraint that handles this exact case, i.e. "version of go must be >= X".

Moving that test into a separate file and adding //+build go1.6 at the top fixed it.

huangapple
  • 本文由 发表于 2016年12月23日 15:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41297073.html
匿名

发表评论

匿名网友

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

确定