使用 Valgrind 运行单元测试是否太过分了?
就在几天前,我开始研究一个名为 check 的单元测试框架, 我打算在Linux下对c代码进行测试。
现在检查一些精心设计的代码和一些测试代码可以帮助我验证 基本功能是正确的, 我的意思是,只需查看变量并返回响应就很容易了 判断一个函数是否正确。
但是假设我想测试一个动态内存结构,其中需要大量调用 malloc 和 free, 事实证明,我可以输入数据并再次取出正确的数据。 但这并不能证明我在这个过程中没有破坏一些记忆, 假设我忘记释放一半内存并丢失了指针(经典的内存泄漏)。 该代码可能会通过大部分单元测试。
现在问题是: 用 ie Valgrind 运行整个单元测试代码并让他是一个好主意吗 检测到任何 malloc/free 问题吗? (或者可能编译成像“电围栏”这样的东西?)
这感觉是个好主意,但我不确定我要在这里做什么......
更新:谢谢道格拉斯和乔纳森, 看来这是一个好主意,我应该继续这样做:-)
更新: Valgrind 是一个有趣的工具,但是我发现这样做的第一个 memleaks 在测试框架中,而不是我自己的代码中(虽然很有趣)。 因此,给其他人的一个提示是,在颠倒自己的代码之前验证您正在使用的单元测试框架没有泄漏。 我的案例只需要一个空的测试用例, 从那时起,除了单元测试框架之外,什么都没有运行。
Just some days ago I started looking into a unit test framework called check,
and I intend to run the test on c code under Linux.
Now check and some well-designed code and some test code can help me to verify
that the basic functionality is correct,
I mean it is quite easy to just look at the variables in and response back and then
decide if a function is correct or not.
But let's say I want to test a dynamic memory structure with a lot of calls to malloc and free,
and it turns out that I can put data in and get correct data back out again.
But that does not prove that I have not broken some memory in the process,
let's say I forgot to free half of the memory and lost the pointers (a classical memleak).
That code would probably pass most of the unit testing.
So now for the question:
is it a good idea to run the entire unit test code with i.e. Valgrind and let him
detect any malloc/free problems? (Or maybe compile in something like Electric Fence?)
It feels like a good idea, but I'm not sure what I'm getting myself into here.....
Update: Thanks Douglas and Jonathan,
it seems like this is a good idea and something I should continue with :-)
Update: Valgrind is a fun tool, however the first memleaks I found doing this
was in the test framework and not my own code (quite funny though).
So, a tip to the rest of you is to verify that the unit test framework you are using is not leaking before turning your own code upside down.
An empty test case was all that was needed in my case,
since then nothing but the unit test framework is running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们当然这样做 - 在单元测试中运行 valgrind 比在完整程序中运行要容易得多。
此外,任何内存错误都被本地化到单元测试正在测试的代码区域,这使得更容易修复。
另外,检查是否已修复它更容易 - 因为您正在运行单元测试,而不是针对整个程序进行更复杂的测试。
如果您以自动方式运行 valgrind,您可能需要
--error-exitcode=; [默认值:0]
http://valgrind.org/docs/manual/manual-core .html#manual-core.errorts
We certainly do - it's much easier to run valgrind against the unit tests than with the full program.
Also any memory errors are localised to the area of code the unit test is testing which makes it easier to fix.
Plus checking that you've fixed it is easier - because you're running the unit test not a more complicated test against your full program.
If you're running valgrind in an automated fashion you probably want
--error-exitcode=<number> [default: 0]
http://valgrind.org/docs/manual/manual-core.html#manual-core.erropts
正如 Douglas Leeder 所说,使用任何您可以掌握的诊断软件来运行单元测试都是非常值得的,这将确保它确实按照您的预期工作。 这包括不滥用内存,因此使用 valgrind 是一个好主意。
您确实希望单元测试能够证明您的代码有效。
您不必一直在 valgrind 下运行它们 - 但这样做应该尽可能简单,并且您应该定期这样做(例如在发生重大更改之后)。
As Douglas Leeder said, it is well worth running your unit tests with any diagnostic software that you can lay hands that will ensure that it really does work as you expect. That includes not abusing memory, so using valgrind is a good idea.
You really want your unit tests to prove that your code works.
You don't have to run them under valgrind all the time - but it should be as trivial as possible to do so, and you should do so periodically (say after big changes).