测试是在Go还是一个一个地执行? [英] Are tests executed in parallel in Go or one by one?

查看:221
本文介绍了测试是在Go还是一个一个地执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单元测试的Go文件,其中一些使用了一个公共变量。另一个全局变量用于我正在测试的代码中。所有这些都可能导致问题。

当我们执行位于同一个文件中的测试时,它们如何运行?平行或下一个在上一个完成之前不会启动?

解决方案

测试它真的很简单:

  func Test1(t * testing.T){
fmt.Println(Test1 start)
time.Sleep(time.Second * 2)
fmt.Println (Test1结束)
}

func Test2(t * testing.T){
fmt.Println(Test2 start)
time.Sleep time.Second * 2)
fmt.Println(Test2 end)
}

func Test3(t * testing.T){
fmt.Println( Test3 start)
time.Sleep(time.Second * 2)
fmt.Println(Test3 end)
}

使用 go test 运行它,输出显示它是连续的:

  Test1开始
Test1结束
Test2开始
Test2结束
Test3开始
Test3结束

所以正常的测试依次执行,但不要忘记没有定义顺序:< a href =https://stackoverflow.com/questions/31201858/how-to-run-golang-te sts-sequential / 31204016#31204016>如何依次运行golang测试?

另外请注意,测试函数可以标记自己适合并行执行,并行执行其他测试也使用 T.Parallel() 方法:


此测试将与(并且仅)

所以如果我们将上面的测试代码修改为:

  func Test1(t * testing.T){
t.Parallel()
fmt.Println(Test1 start)
time .Sleep(time.Second * 2)
fmt.Println(Test1 end)
}

func Test2(t * testing.T){
t .Parallel()
fmt.Println(Test2 start)
time.Sleep(time.Second * 2)
fmt.Println(Test2 end)
}

func Test3(t * testing.T){
fmt.Println(Test3 s tart)
time.Sleep(time.Second * 2)
fmt.Println(Test3 end)
}

再次使用 go test 运行它,输出结果为:

  Test3开始
Test3结束
Test1开始
Test2开始
Test2结束
Test1结束

这证明了什么?测试顺序未定义,这次 Test3 被执行。然后运行 Test1 Test2 并行。



有一些控制并行执行的测试标志。例如, -parallel 标志指定其中有多少可以并行运行。如果使用 go test -parallel = 1 执行它,输出将再次成为顺序,但顺序将为 Test3 Test1 Test2 Go 1.7引入了子测试和子基准。您可以在使用子测试和子基准 b
$ b


在Go 1.7中,测试包在 T B 类型,允许创建子测试和子基准。引入子测试和子基准可以更好地处理故障,对从命令行运行哪些测试进行细化控制,并行控制,并且通常会产生更简单且更易维护的代码。

子测试和子基准可以并行运行,并且有一些标志可以控制它们的执行,例如 -parallel -p -cpu 。运行 go help testflag 查看测试标志的完整列表。


I have a Go file with unit tests and some of them use a common variable. Another global variable is used in the code that I'm testing. All of this can cause a problem.

In Go when we execute tests that are located in the same file how does they run? In parallel or next one will not start before previous one is finished ?

解决方案

It's really easy to test it:

func Test1(t *testing.T) {
    fmt.Println("Test1 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test1 end")
}

func Test2(t *testing.T) {
    fmt.Println("Test2 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test2 end")
}

func Test3(t *testing.T) {
    fmt.Println("Test3 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test3 end")
}

Running it with go test, the output shows it's sequential:

Test1 start
Test1 end
Test2 start
Test2 end
Test3 start
Test3 end

So normal tests are executed one after the other, sequentially, but don't forget that order is not defined: How to run golang tests sequentially?

Also note that a test function can mark itself eligible for parallel execution, paralell with other tests that also do the same using the T.Parallel() method:

Parallel signals that this test is to be run in parallel with (and only with) other parallel tests.

So if we modify the above testing code to:

func Test1(t *testing.T) {
    t.Parallel()
    fmt.Println("Test1 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test1 end")
}

func Test2(t *testing.T) {
    t.Parallel()
    fmt.Println("Test2 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test2 end")
}

func Test3(t *testing.T) {
    fmt.Println("Test3 start")
    time.Sleep(time.Second * 2)
    fmt.Println("Test3 end")
}

Running it again with go test, the output is:

Test3 start
Test3 end
Test1 start
Test2 start
Test2 end
Test1 end

What does this prove? The order of tests is not defined, Test3 was executed first this time. And then Test1 and Test2 were run parallel.

There are some testing flags that control parallel execution. For example the -parallel flag specifies how many of these may run parallel. If you execute it with go test -parallel=1, the output will again become sequential, but the order will be Test3, Test1, Test2.

Also note that Go 1.7 introduced subtests and subbenchmarks. You can read more about this in blog post Using Subtests and Sub-benchmarks:

In Go 1.7, the testing package introduces a Run method on the T and B types that allows for the creation of subtests and sub-benchmarks. The introduction of subtests and sub-benchmarks enables better handling of failures, fine-grained control of which tests to run from the command line, control of parallelism, and often results in simpler and more maintainable code.

Subtests and subbenchmarks may run parallel, and there are a number of flags that may control their execution, e.g. -parallel, -p, -cpu. Run go help testflag to see the complete list of testing flags.

这篇关于测试是在Go还是一个一个地执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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