简体   繁体   English

Googletest:如何异步运行测试?

[英]Googletest: How to run tests asynchronously?

Given a large project with thousands of tests, some of which take multiple minutes to complete. 给定一个具有数千个测试的大型项目,其中一些测试需要几分钟才能完成。 When executed sequentially, the whole set of test takes more than an hour to finish. 按顺序执行时,整套测试需要一个多小时才能完成。 The testing time could be reduced by executing tests in parallel. 通过并行执行测试,可以减少测试时间。

As far as I know there are no means to do that directly from googletest/mock, like a --async option. 据我所知,没有办法直接从googletest / mock进行此操作,例如--async选项。 Or am I wrong? 还是我错了?

One solution is to determine the tests which can run in parallel and write a script that starts each in a separate job, ie 一种解决方案是确定可以并行运行的测试,并编写一个脚本以在单独的作业中启动每个脚本,即

./test --gtest_filter=TestSet.test1 &
./test --gtest_filter=TestSet.test2 &
...

But this would require additional maintenance effort and introduce another "layer" between the test code and its execution. 但这将需要额外的维护工作,并在测试代码与其执行之间引入另一个“层”。 I'd like a more convenient solution. 我想要一个更方便的解决方案。 For example, one could suffix the TEST and TEST_F macros and introduce TEST_ASYNC, TEST_F_ASYNC. 例如,可以为TEST和TEST_F宏加上后缀,并引入TEST_ASYNC,TEST_F_ASYNC。 Tests defined with TEST_ASYNC would then be executed by independent threads, starting at the same time. 然后,由TEST_ASYNC定义的测试将由独立线程同时开始执行。

How can this be achieved? 如何做到这一点? Or is there another solution? 还是有其他解决方案?

Late response, but I'll put it here for anyone searching for a similar answer. 回复较晚,但我会将其放在此处,以供所有寻求类似答案的人使用。 Working on WebRTC I found a similar need to speed up our test execution. 在WebRTC上工作时,我发现有类似的需求来加快测试执行速度。 Executing all of our tests sequentially took more than 20 minutes, and a bunch of them spend at least some time waiting (so they don't even fully utilize a core). 按顺序执行我们所有的测试都花费了20多个分钟,并且其中许多人至少花了一些时间等待(因此他们甚至没有充分利用内核)。

Even for "proper unit tests" I'd argue this is still relevant, because there's a difference between your single-threaded tests taking 20 seconds and ~1 second to execute (if your workstation is massively parallel, this speedup is not uncommon). 即使对于“适当的单元测试”,我也认为这仍然是有意义的,因为执行单线程测试需要20秒和大约1秒的时间之间存在差异(如果您的工作站是大规模并行的,则这种加速并不罕见)。

To solve this for us, I developed a script that test executes tests in parallel. 为了为我们解决这个问题,我开发了一个脚本,该脚本可以并行执行测试。 This is stable enough to run on our continuous integration, and released here: https://github.com/google/gtest-parallel/ 这足够稳定,可以在我们的持续集成中运行,并在此处发布: https : //github.com/google/gtest-parallel/

This python script essentially takes --gtest_filter=Foo (which you can specify) of one or more gtest binary specified, splits them up on several workers and runs individual tests in parallel. 这个python脚本实质上采用--gtest_filter=Foo (您可以指定)指定的一个或多个gtest二进制文件,将它们拆分为多个工作程序并并行运行各个测试。 This works fine so long as the tests are independent (don't write to shared files, etc). 只要测试是独立的(不要写入共享文件等),此方法就可以正常工作。 For tests that didn't work fine, we put them in a webrtc_nonparallel_tests binary and ran those separately, but the vast majority were already fine, and we fixed several of them because we wanted the speedup. 对于无法正常运行的测试,我们将它们放在webrtc_nonparallel_tests二进制文件中,然后分别运行它们,但是绝大多数已经很好,并且我们修复了其中的几个,因为我们希望加快速度。

I would suggest you are solving the wrong problem. 我建议您正在解决错误的问题。 You want unit tests to run quickly, in fact if a test takes several minutes to run, it's not a unit test. 您希望单元测试能够快速运行,实际上,如果测试需要几分钟才能运行,则它不是单元测试。

I suggest you split your tests into proper unit tests and integration/regression or slow running tests. 我建议您将测试分为适当的单元测试和集成/回归或运行缓慢的测试。 You can then run the unit tests as you develop and just run the longer running ones before a push/commit. 然后,您可以在开发过程中运行单元测试,而在推送/提交之前只运行运行时间更长的单元测试。

You could even run the two (or more) sets of tests yourself simultaneously. 您甚至可以自己同时运行两组(或更多组)测试。

The docs themselves suggest using filters to solve this. 文档本身建议使用过滤器来解决此问题。


Edit in light of a downvote, and a new toy mentioned in the docs. 请根据票证和文档中提到的新玩具进行编辑。

Since I gave this answer, the docs have been updated and now mention a parallel runner , which "works by listing the tests of each binary, and then executing them on workers in separate processes" which would solve the problem. 自从我给出这个答案以来,文档已经更新,现在提到了并行运行程序 ,它“通过列出每个二进制文件的测试,然后在单独的进程中的工作程序上执行测试来工作”,这可以解决问题。 When I first wrote the answer this didn't exist. 当我第一次写答案时,这个还不存在。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM