使用 Valgrind 运行单元测试是否太过分了?

发布于 2024-07-13 06:01:35 字数 693 浏览 9 评论 0原文

就在几天前,我开始研究一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哑剧 2024-07-20 06:01:35

我们当然这样做 - 在单元测试中运行 valgrind 比在完整程序中运行要容易得多。

此外,任何内存错误都被本地化到单元测试正在测试的代码区域,这使得更容易修复。

另外,检查是否已修复它更容易 - 因为您正在运行单元测试,而不是针对整个程序进行更复杂的测试。

如果您以自动方式运行 valgrind,您可能需要 --error-exitcode=; [默认值:0]

指定在 Valgrind 报告任何情况时返回的备用退出代码
运行中的错误。 当设置为
默认值(零),返回值
Valgrind 永远是
进程的返回值是
模拟的。 当设置为非零时
value,而是返回该值,
如果 Valgrind 检测到任何错误。 这
对于使用 Valgrind 作为一部分很有用
自动化测试套件,因为它
可以轻松检测测试用例
Valgrind 报告了错误,
只需检查返回代码即可。

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]

Specifies an alternative exit code to return if Valgrind reported any
errors in the run. When set to the
default value (zero), the return value
from Valgrind will always be the
return value of the process being
simulated. When set to a nonzero
value, that value is returned instead,
if Valgrind detects any errors. This
is useful for using Valgrind as part
of an automated test suite, since it
makes it easy to detect test cases for
which Valgrind has reported errors,
just by inspecting return codes.

http://valgrind.org/docs/manual/manual-core.html#manual-core.erropts

草莓酥 2024-07-20 06:01:35

正如 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文