简体   繁体   English

如何测试断言?

[英]How to test assertions?

I am using a unit testing framework to test my libraries.我正在使用单元测试框架来测试我的库。 I have quite a few assertions in the library to make sure that programmer errors are caught in debug builds.我在库中有很多断言,以确保在调试版本中捕获程序员错误。 Now I want to make sure I am testing for all possible programmer errors.现在我想确保我正在测试所有可能的程序员错误。

Eg in a Table class, I want to make sure that the rows and cols passes are not greater than the rows and cols the table has.例如,在表 class 中,我想确保行和列传递不大于表具有的行和列。 Let's assume I forget to test for cols.假设我忘记测试 cols。 I would like to have my unit tests perform a test where the assertion should fire, and if not, fail the test.我想让我的单元测试在断言应该触发的地方执行测试,如果没有,则测试失败。 Is that possible?那可能吗?

The question then becomes are you planning on refactoring your code so that if this condition occurs you'll throw an exception instead of relying on <cassert> functionality to clue you into a problem?那么问题就变成了您是否计划重构您的代码,以便如果发生这种情况,您将抛出异常而不是依赖<cassert>功能来提示您解决问题? If so, you can just check that the exception was thrown.如果是这样,您只需检查是否引发了异常。 If not, then it's going to be more difficult to test an assert statement from <cassert> .如果不是,那么从<cassert>测试断言语句将更加困难。 Unit test frameworks like CUTE have an ASSERT_THROWS macro just for exception testing.像 CUTE 这样的单元测试框架有一个ASSERT_THROWS宏,仅用于异常测试。 I'd check your framework.我会检查你的框架。

Also, it's been the case with the shops where I've worked that they frown upon assert and prefer exceptions.此外,我工作过的商店就是这种情况,他们不赞成断言,更喜欢例外。 Calling abort doesn't help automated testing.调用abort无助于自动化测试。 Actually, it prohibits it.实际上,它禁止它。 Just my two cents.只是我的两分钱。

There are three possible ways:有三种可能的方式:

  • converting assertions to exceptions, or将断言转换为异常,或

  • running each test a separate program whose exit code you (or rather, the test framework) checks, or运行每个测试一个单独的程序,您(或者更确切地说,测试框架)检查其退出代码,或者

  • let an assertion that fires, write information about itself, which the test framework then can pick up.让一个触发的断言写入有关自身的信息,然后测试框架可以获取这些信息。

As far as I know no commercial or widely used unit test framework supports the last two ways.据我所知,没有商业或广泛使用的单元测试框架支持最后两种方式。 I have used checking of process exit code for hobby programming, but only with a small personal unit test framework implemented in Python and C++ ("hobby programming": that means I have no good data on how well it scales to large scale programming).我已经使用检查进程退出代码来进行爱好编程,但仅使用在 Python 和 C++ 中实现的小型个人单元测试框架(“爱好编程”:这意味着我没有关于它如何扩展到大规模编程的良好数据)。 The main case for process exit code testing is where the code has static assertions, that you want to be sure are triggered when they are intended to be.进程退出代码测试的主要情况是代码具有 static 断言,您希望确保它们在预期时被触发。

Summing up, with extant test frameworks, conversion to exception is AFAIK your only option.总而言之,使用现有的测试框架,转换为异常是 AFAIK 您唯一的选择。

Cheers & hth.干杯&hth。

The answer is dependent on the particular test framework that you are using, and will probably be found with very little searching in google.答案取决于您使用的特定测试框架,并且很可能在 google 中很少搜索就可以找到。

The first hit for "boost unittest test assert" points to this question in StackOverflow: Testing for assert in the Boost Test framework “boost unittest test assert”的第一次点击指向StackOverflow中的这个问题: Testing for assert in the Boost Test framework

The second hit for "cppunit test assert" points to this documentation page: Making Assertions “cppunit test assert”的第二次点击指向此文档页面: Making Assertions

Try searching in the internet for your concrete framework.尝试在互联网上搜索您的具体框架。

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

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