简体   繁体   English

go 测试是否并发运行单元测试?

[英]Does go test run unit tests concurrently?

When go test is ran it runs your files ending in _test.go by running the functions that start in the format TestXxx and use the (*t testing.T) module.当运行go test时,它会通过运行以TestXxx格式开始并使用 (*t testing.T) 模块的函数来运行以_test.go结尾的文件。 I was wondering if each function in the _test.go file were ran concurrently or if it definitively ran each function separately?我想知道_test.go文件中的每个 function 是否同时运行,或者它是否明确地分别运行每个 function? Does it create a go routine for each one?它是否为每个程序创建一个 go 例程? If it does create a go routine for each one, can I monitor the go routines in some way?如果它确实为每个创建一个 go 例程,我可以以某种方式监视 go 例程吗? Is it ever possible to do something like golibrary.GoRoutines() and get an instance for each one and monitor them some how or something like that?是否有可能做类似golibrary.GoRoutines()的事情并为每个实例获取一个实例并以某种方式或类似方式监视它们?


Note: this question assumes you using the testing framework that comes with go (testing).注意:此问题假设您使用 go(测试)附带的测试框架。

Yes , tests are executed as goroutines and, thus, executed concurrently .是的测试是作为 goroutines 执行的,因此是并发执行的。

However, tests do not run in parallel by default as pointed out by @jacobsa .但是,正如@jacobsa指出的那样,默认情况下测试不会并行运行 To enable parallel execution you would have to call t.Parallel() in your test case and set GOMAXPROCS appropriately or supply -parallel N (which is set to GOMAXPROCS by default).要启用并行执行,您必须在测试用例中调用t.Parallel()并适当设置GOMAXPROCS或提供-parallel N (默认设置为GOMAXPROCS )。

The simplest solution for your case when running tests in parallel would be to have a global slice for port numbers and a global atomically incremented index that serves as association between test and port from the slice.在并行运行测试时,最简单的解决方案是拥有一个端口号的全局切片和一个全局原子递增索引,作为测试和切片端口之间的关联。 That way you control the port numbers and have one port for each test.这样您就可以控制端口号并为每个测试指定一个端口。 Example:例子:

import "sync/atomic"

var ports [...]uint64 = {10, 5, 55}
var portIndex uint32

func nextPort() uint32 {
    return atomic.AddUint32(&portIndex, 1)
}

Yes, as other answers already pointed, tests inside one _test.go file are running in parallel only if you add t.Parallel() and run with -parallel tag.是的,正如其他答案已经指出的那样,只有在添加t.Parallel()并使用-parallel标记运行时,一个 _test.go 文件中的测试才会并行运行。

BUT - by default the command go test runs in parallel tests for different packages, and default is the number of CPUs available (see go help build )但是 - 默认情况下, go test命令在不同包的并行测试中运行,默认是可用的 CPU 数量(请参阅go help build

So to disable parallel execution you should run tests like this go test -p 1 ./...因此,要禁用并行执行,您应该运行这样的测试go test -p 1 ./...

Yes if you add this : t.Parallel() to your functions.是的,如果您将这个 : t.Parallel() 添加到您的函数中。

like this:像这样:

func TestMyFunction(t *testing.T) {
    t.Parallel()
    //your test code here

}

There are two aspects of "parallel tests" in go. go中的“并行测试”有两个方面。

  1. Testing of packages (folders) in parallel.并行测试包(文件夹)。 Controlled by -p flag.-p标志控制。

go help build : go help build

    -p n
        the number of programs, such as build commands or
        test binaries, that can be run in parallel.
        The default is GOMAXPROCS, normally the number of CPUs available.
  1. Testing of tests within a package. Controlled by -parallel flag, and tests need this enabled with t.Parallel() .在 package 内测试测试。由-parallel标志控制,并且测试需要使用t.Parallel()启用此功能。

go help testflag : go help testflag

    -parallel n
        Allow parallel execution of test functions that call t.Parallel.
        The value of this flag is the maximum number of tests to run
        simultaneously; by default, it is set to the value of GOMAXPROCS.
        Note that -parallel only applies within a single test binary.
        The 'go test' command may run tests for different packages
        in parallel as well, according to the setting of the -p flag
        (see 'go help build').

For #1, you can observe this directly by having tests in multiple packages, and forcing them to be long running with a sleep.对于 #1,您可以通过在多个包中进行测试并强制它们在休眠状态下长时间运行来直接观察到这一点。 Then ps aux and grep for go running and you'll see separate processes in parallel.然后 ps aux 和 grep 运行 go,您将看到并行的独立进程。

Or run with -x detail flag.或者使用-x detail 标志运行。 That pretty clearly shows what's going on.这非常清楚地表明了正在发生的事情。 The tests are launched once per package (eg given have *_test.go files in several package. Note I'm running go test -x./... -v -count=1 |& ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")' |& tee temp.out with various configurations of -p=N -parallel=M, you can see in parallel them running:测试每 package 启动一次(例如,在几个 package 中有 *_test.go 文件。注意我正在运行go test -x./... -v -count=1 |& ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")' |& tee temp.out各种配置的-p=N -parallel=M,你可以看到并行跑步:

[2022-05-09 17:22:26] $WORK/b065/understandinit.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1
[2022-05-09 17:22:26] $WORK/b001/main.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1
[2022-05-09 17:22:26] $WORK/b062/mainsub.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1

You can run them concurrently by flagging the test with t.Parallel and then run the test using the -parallel flag.您可以通过使用t.Parallel标记测试来同时运行它们,然后使用-parallel标志运行测试。

You can see other testing flags here您可以在此处查看其他测试标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio不会在测试类中运行所有单元测试 - Visual Studio does not run all the unit tests in a test class TfsBuild不运行单元测试 - TfsBuild does not run unit tests VueJS 单元测试 - 我无法运行我的测试。 “命令‘测试/单元’不存在” - VueJS Unit testing - I cannot run my tests. "Command 'test/unit' does not exists" 如何使用测试特定的类运行单元测试 - How to run unit tests with test specific class 是否可以只运行带有货物测试的单元测试? - Is it possible to run only unit tests with cargo test? 如何同时运行NUnit测试? - How to run concurrently NUnit tests? 为什么在与其他测试并行运行时,此Swift单元测试会崩溃? - Why does this Swift unit test crash when run in parallel with other tests? Azure DevOps 服务器:为什么 Visual Studio 测试步骤需要这么长时间才能运行单元测试? - Azure DevOps Server: Why does a Visual Studio Test step take so long to run unit tests? Visual Studio 2012中的Windows 8应用程序-NUnit测试适配器可以看到单元测试,但不能运行它们 - Windows 8 App in Visual Studio 2012 - NUnit Test Adapter sees unit tests but does not run them 在代码中运行Ruby单元测试(Test :: Unit) - Run Ruby Unit Tests(Test::Unit) inside the Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM