dongshuql24533 2017-06-02 09:19
浏览 308
已采纳

是在Go中并行执行测试还是一一执行?

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 ?

  • 写回答

2条回答 默认 最新

  • doupang5433 2017-06-02 10:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开