Googletest:如何异步运行测试? [英] Googletest: How to run tests asynchronously?

查看:377
本文介绍了Googletest:如何异步运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个包含数千个测试的大型项目,其中一些测试需要花费几分钟的时间才能完成.按顺序执行时,整套测试需要一个多小时才能完成.通过并行执行测试可以减少测试时间.

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.

据我所知,没有办法直接通过googletest/mock进行操作,例如-async 选项.还是我错了?

As far as I know there are no means to do that directly from googletest/mock, like a --async option. 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, i.e.

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

但这将需要额外的维护工作,并在测试代码与其执行之间引入另一个层".我想要一个更方便的解决方案.例如,可以为TEST和TEST_F宏加上后缀,并引入TEST_ASYNC,TEST_F_ASYNC.然后,由TEST_ASYNC定义的测试将由独立的线程同时开始执行.

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. Tests defined with TEST_ASYNC would then be executed by independent threads, starting at the same time.

如何实现?还是有其他解决方案?

How can this be achieved? Or is there another solution?

推荐答案

我建议您正在解决错误的问题.您希望 unit 测试能够快速运行,实际上,如果测试需要几分钟才能运行,则它不是单元测试.

我建议您将测试分为适当的单元测试和集成/回归或运行缓慢的测试.然后,您可以在开发过程中运行单元测试,而在推送/提交之前只运行运行时间更长的单元测试.

您甚至可以自己同时运行两组(或更多组)测试.

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.

请编辑并参考文档中提到的新玩具.

自从我给出了答案以来,文档已经更新,现在提到了并行运行器,通过列出每个二进制文件的测试,然后在单独的进程中的工作程序上执行它们的工作方式",可以解决该问题.当我第一次写答案时,这个还不存在.

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.

这篇关于Googletest:如何异步运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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