使用CppUnit参数化测试 [英] Parameterizing a test using CppUnit

查看:293
本文介绍了使用CppUnit参数化测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组织正在使用CppUnit,我试图使用不同的参数运行相同的测试。在测试中运行循环不是一个好的选择,因为任何失败都将中止测试。我看过 TestDecorator TestCaller ,但似乎都不适合。

My organization is using CppUnit and I am trying to run the same test using different parameters. Running a loop inside the test is not a good option as any failure will abort the test. I have looked at TestDecorator and TestCaller but neither seems to really fit. Code samples would be helpful.

推荐答案

它不可能在CppUnit中直接参数化测试用例(参见在这里在这里)。但您有以下几种选择:

It does not appear possible in CppUnit to parameterize a test case directly (see here and here). However, you do have a few options:

您可以使用内置的 RepeatedTest 装饰器。这允许测试用例多次运行(虽然没有参数化)。

You may be able to make some clever use of the built-in RepeatedTest decorator. This allows a test case to be run multiple times (though without parameterization).

我承认从来没有使用过这个,但也许你可以有 RepeatedTest 驱动一些关守函数,这将(使用类静态变量,也许?)每次运行选择一个不同的输入。它会反过来调用你想用该值作为输入测试的真正的函数。

I'll admit to never having used this myself, but perhaps you could have the RepeatedTest drive some gatekeeper function, which would (using a class static variable, perhaps?) pick a different input with every run. It would in turn call the true function you'd like to test with that value as input.

一个人在CppUnit的SourceForge页面声称已经写了一个 TestCase 的子类,它将运行一个特定的测试任意次数,虽然稍微不同于 RepeatedTest 课程优惠。可悲的是,海报只是描述了创建类的动机,但没有提供源代码。

One person on CppUnit's SourceForge page claims to have written a subclass of TestCase that will run a particular test an arbitrary number of times, although in a slightly different manner than the RepeatedTest class offers. Sadly, the poster simply described the motivation for creating the class, but did not provide the source code. There was, however, an offer to contact the individual for more details.

最直接(但最不自动)的方法是创建一个帮助函数,它将需要传递的参数传递给真实函数,然后有很多个别的测试用例。

The most straight-forward (but least automated) way to do this is to create a helper function that takes the parameter you'd like to pass on to your "real" function, and then have lots of individual test cases. Each test case would call your helper function with a different value.


如果您选择上述前两个选项中的任一个,我有兴趣听取您的经验。

If you choose either of the first two options listed above, I'd be interested in hearing about your experience.

这篇关于使用CppUnit参数化测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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