如何在Go中编写使用-short标志的测试并将其与-benchmark标志结合使用? [英] How do I write test in Go that make use of the -short flag and can it be combined with the -benchmark flag?

查看:119
本文介绍了如何在Go中编写使用-short标志的测试并将其与-benchmark标志结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用go test -short中给出的-short标志?

How do I make use of the -short flag given in go test -short?

是否可以组合-short-benchmark标志?

Is it possible to combine the -short and -benchmark flags?

我对Go语言很陌生,但是我正在努力使自己适应其中的一些常见习惯.这部分是为了确保我的代码不仅以go test系统工作的方式添加了单元测试,而且go test -benchmark也以有用的方式工作.

I am quite new to the Go language but I am trying to adapt myself to some of its common practises. Part of this is to try and ensure my code has not only Unit Tests added in a way that the go test system works but that go test -benchmark also functions in a useful manner.

目前,我有一个基准测试,其中包括一系列基于不同大小的输入数据的子测试.运行15个排列需要很长时间,因此最好选择缩短测试时间的方法.

At the moment I have a benchmark test that includes a series of sub-tests based on varying sized of input data. Running the 15 permutations takes a long time, so it would be nice to given an option of shortening that test time.

我计划编写的下一组测试可能包括一系列数据输入示例.我希望只运行其中一个就可以作为简短测试的健全性检查,但是可以选择在更长(或正常)的测试中运行多个测试.

The next set of tests I plan to write could include a series of data input examples. I expect running a single one of these could work as a sanity check for a short test but having the option to run several on longer (or normal) test runs would be good.

当我查看测试标记的GoLang文档时,它说告诉长时间运行的测试以缩短其运行时间."听起来像我想要的,但是我无法确定如何在测试代码中选择此标志.

When I look at the GoLang documentation for the testing flags it says "Tell long-running tests to shorten their run time." which sounds like what I want, but I cannot work out how to pick up this flag in the test code.

推荐答案

我如何利用go test -short中提供的-short标志?

How do I make use of the -short flag given in go test -short?

在命令行上使用short标志会使testing.Short()函数返回true.您可以使用它添加测试用例或跳过它们:

Using the short flag on the command line causes the testing.Short() function to return true. You can use this to either add test cases or skip them:

if testing.Short() == false {
    // Extra test code here
}

上面的说法有点不合常理,可能更常见于:

The above is perhase a little unusal, it may be more common to see:

func TestThatIsLong(t *testing.T) {
    if testing.Short() {
        t.Skip()
    }
}

确保为-short运行提供足够的测试用例,以至少进行最小限度的检查.有人建议将-short运行用于主要的持续集成和预提交检查,同时保留较长的测试运行以进行计划的每日或每周构建或合并.

Be sure to have enough test cases for your -short runs to do at least a bare minimal check. Some people suggest using the -short run for the main continuous integration and pre-commit checks while keeping the longer test runs for scheduled daily or weekly builds or merges.

"Go编程语言"网站的文档部分简要介绍了如何编写测试代码,但大部分内容有关该主题的信息,请参见 Go测试软件包的软件包文档.对于大多数主题,大多数文档都可以在软件包中找到,而不是单独找到.这可能与其他语言相比,后者的类和包文档通常很差.

The Documents section of The Go Programming Language site mentions how to write test code briefly but the bulk of information about the topic is in the Package Documentation for the Go Testing Package. For most topics, the majority of documentation will be found in the package rather than separate. This may be quite different from other languages where the class and package documents are often poor.

是否可以结合使用-short和-benchmark标志?

Is it possible to combine the -short and -benchmark flags?

testing.Short()可能是全局范围.但是,建议基准测试不要大量使用-short标志来控制其行为.对于运行基准测试的人来说,更改每个基准测试用例允许的-benchtime更为常见.

It is possible as the testing.Short() is global in scope. However, it is recommended that Benchmark tests do not make extensive use of the -short flag to control their behaviour. It is more common for the person running the benchmarking to alter the -benchtime permitted for each benchmark test case.

默认情况下,工作时间设置为一秒.如果您有60个基准测试用例,则至少需要60秒才能完成运行(设置时间+执行时间).如果将工作时间设置为更少:

By default, the benchtime is set to one second. If you have 60 benchmark test cases, it will take at least sixty seconds to complete the run (setup time + execution time). If the benchtime is set to less:

go test -benchmem -benchtime 0.5s -bench=. <package_names>

总体执行时间将成比例下降.

the overall execution time will go down proportionality.

go命令文档的测试标志"部分中介绍了各种测试标记(不是没有提及基准测试时间的软件包文档).您无需对基准代码进行任何其他操作即可使基准测试有效,只需使用标准for i := 0; i < b.N; i++ {,该框架就会根据需要调整N的值.

The various testing clags are described in the go command documentation's Testing Flags section (not the package documentation which does not mention benchtime). You do not need to do anything different in your benchmark code to have the benchtime be effective, just use the standard for i := 0; i < b.N; i++ { and the framework will adjust the value of N as needed.

尽管不建议这样做,但在基准测试中使用-short可以减少更改函数输入以表示其

Although it is not recommended, I have used -short within a benchmark to reduce the number of test cases when varying input to a function to give an indication of its Big O notation. For all benchmark runs (-short and normal), I keep a representative data size for the input to track long term trends. For the longer runs, I include several small and larger sized data sets to allow an approximation of the functions resource requirements. As with the unit test cases I choose to run the -short version always in the CI and the longer version on a schedule.

无论您对Go有什么疑问,强烈建议您仔细阅读两者 https://golang.org/doc/和相关的 https://golang.org/pkg/文档.通常,最有用的文档在软件包文档中.

Whatever your questions are with Go, it is strongly recommended that you try thoroughly reading both the https://golang.org/doc/ and relevant https://golang.org/pkg/ documents. Frequently the most useful documentation is in the package documentation.

这篇关于如何在Go中编写使用-short标志的测试并将其与-benchmark标志结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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