dptt66700 2019-03-15 10:32
浏览 84

如何在Go中编写使用-short标志的测试并将其与-benchmark标志结合使用?

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

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

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.

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.

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.

  • 写回答

1条回答 默认 最新

  • douqiao7188 2019-03-19 10:04
    关注

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

    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()
        }
    }
    

    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.

    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.


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

    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.

    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.

    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.

    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.


    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题