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 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!