不返回任何值的函数的单元测试 [英] Unit testing of a function that doesn't return any value

查看:50
本文介绍了不返回任何值的函数的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cppunit 在 C++ 中进行单元测试.我在互联网上看到了一些示例,这些示例显示测试返回某个值的函数.我的项目中有一些函数不返回任何值,但在内部进行一些计算.在这里,我想测试这些函数中的计算是否正确完成了计算,我该怎么做?例如我有一个函数

I am doing unit testing in c++ using cppunit. I have seen some examples over internet that are showing testing a function that return some value. I have some functions in my project that do not return any value but do some calculations inside. Here I want to test the calculations inside those functions whether calculations are being done correctly or not, how can I do it? For example I have a function

void Flash::SerializeToBytes(Buffer &buffer) const
{
    // calculations
} 

推荐答案

这些自动测试的一般概念总是相同的:您知道,给定某个输入,函数应该产生某个结果.将实际结果与您期望的结果进行比较.如果它们相同,则测试通过,否则您的代码或测试中有错误.

The general concept of these automatic tests is always the same: you know that, given a certain input, a function should yield a certain result. Compare the actual result with the one you expect. If they are the same, the test is passed, otherwise you either have a bug in your code or in your test.

也就是说,让我们尝试将其应用于您的特定案例.您通过引用传递 buffer,以便您可以修改它(否则它应该是一个常量引用!).因此,您必须编写调用函数的测试,然后检查 buffer 发生了什么,而不是检查返回值.从概念上讲,它是相同的:您提供一些输入并将输出与预期的输出进行比较.只是在这种情况下,输出不是返回值,而是用作输入的同一个对象.

That said, let's try to apply it to your specific case. You are passing buffer by reference, so that you can modify it (otherwise it should be a const reference!). So you have to write tests that call your function and then check what happened to buffer, instead of checking the returned value. Conceptually it's the same: you provide some input and compare the output with the expected one. It's just that in this case the output isn't a returned value, but rather the same object used as input.

如果这是不可能的,例如因为参数是通过常量引用或值传递的,您必须了解您的函数如何与世界其他地方交互.如果它不返回、抛出、修改输入参数之一等,那么它就不是这些测试的良好候选者.这可能意味着两件事:它是一个您不关心测试的函数(至少在这种情况下),或者您必须重构您的代码.

In case this is not possible, for example because the arguments are passed by const reference or by value, you must understand how your function interacts with the rest of the world. If it doesn't return, throw, modify one of the input arguments, etc, then it is not a good candidate for these tests. These could mean 2 things: that it is a function you don't care about testing (at least in this way), or that you must refactor your code.

前者的一个例子是与硬件交互的东西.假设您正在为一些带有 LED 的嵌入式系统编写代码,并且您具有打开或关闭 LED 的功能.这不是一个适合自动测试的案例.跳过它,您不需要用自动测试覆盖 100% 的代码(请参阅这个很好的答案).

An example of the former would be something that interacts with hardware. Suppose you are writing code for some embedded system with a LED, and you have a function to turn the LED on or off. This is not a case that lends itself to automatic tests. Just skip it, you don't need to cover 100% of your code with automatic tests (see this great answer).

另一方面,由于全局变量,您的程序可能会大量使用全局变量(可怕的想法),因此您的函数可能会与世界其他地方进行交互.那么在这种情况下,您仍然可以编写一些测试(只需检查调用函数后全局变量发生了什么变化),但是您应该做的是重构您的代码以便您的函数接收它需要的所有变量作为参数,并且在为所有函数完成此操作后,您可以将全局变量更改为非全局变量,最终得到返回值和/或修改作为引用或指针传递的参数,编写测试很容易.

On the other hand, maybe your function interacts with the rest of the world thanks to global variables, which your program makes heavy use of (terrible idea). Well in this case you could still write some tests (just check what happens to the global variables after you have called the function), but what you should do is refactoring your code so that your function receives as argument all the variables that it needs, and after you've done it for all the functions, you can change the global variables to non-global, and you end up with functions that return a value and/or modify the arguments passed as reference or pointer, for which writing tests is easy.

简而言之:如果您有一个不知道如何测试的函数,那么可能要么不值得为它编写测试,要么表明您的代码可能需要进行一些更改.

To make it short: if you have a function that you don't know how to test, then probably either writing tests for it isn't worth it, or it is an indicator that your code might require some changes.

这篇关于不返回任何值的函数的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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