C++ 中的回归测试
我无法对代码的某些部分使用单元测试,因此我转而使用回归测试。 我想检查我的程序在进行一些修改后是否以相同的方式运行。 我所说的行为主要是指数据结构的状态。 到目前为止,我正在将它们序列化为人类可读的文本格式,并在第一次运行时转储到一些文件中。 然后在下一次转储中我可以比较状态是否发生变化。 如果更改来自新功能而不是错误,则更新它。
我可以使用一个库(C++)来组织所有这些。 你知道任何? 与转储文件一起,它将提供廉价、大规模的单元测试。
最麻烦的是序列化过程。 有时我只是转储内存状态,但当它不同时,很难进行逆向工程。 所以我转向了另一种方法。 现在,在比较阶段,我将内存转储读取到“幻像”对象中,并运行专门的 diff 方法(具有丰富错误报告的运算符 == ),这有时比序列化为人类可读的文本格式更容易编写。
基本上我想重新发明轮子,所以我的问题很笼统:
您如何执行回归测试(如果执行)?
你使用任何库/工具包吗?
您是否曾根据自己的需要实施过一种方案?
出于好奇:
您是否曾经想做回归测试,但有什么事情阻止了您?
I can't use unit tests for some parts of code so I'm falling back to regression tests.
I would like to check whether my program behaves in the same way after some modifications.
And by behaviour I mean mostly a state of data structures.
So far I was serializing them into human readable text format and dumped to some files in the first run. Then in the next dumps I could compare whether the state changed or not.
And update it if the change comes from a new feature and not from a bug.
I could use a library (C++) to organize all that. Do you know any?
Together with dump files it would provide a cheap, massive unit-test.
The most cumbersome thing are the serialization procedures. Sometimes I just dump memory state, but when it is different it's hard to reverse engineer. So I moved to another method. Now, during the compare phase I read a memory dump into a "phantom" object and run a specialized diff method (operator== with rich error reporting), which sometimes is easier to write than serializing to human readable text format.
Basically I feel like reinventing the wheel, so my questions are quite general:
How do you perform regression testing (if you do)?
Do you use any library/toolkit?
Have you ever implemented one for your own needs?
And just out of curiosity:
Have you ever wanted to do regression testing, but something stopped you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看 Boost 测试库
。 我从未使用过它,但它可能会满足你的愿望。
当我工作时,我通常在测试跟踪器中编写简单的测试用例,以使回归测试尽可能简单(特别是对于非我的人:)。
You might take a look at the Boost Test Library
. I have never used it, but it might fulfill your desires.
When i am working i usually write simple test cases in our test-tracker to make regression tests as easy as possible (especially for not-me people :).
查看 boost 序列化 库。 这将允许您将文件转储到 xml,然后您可以对每个版本进行比较。
为了创建测试用例,如果您还没有这样做,请使用 gcov 来计算函数的覆盖范围。 这至少可以确保您的函数中涵盖了所有语句。
单元测试
当我们测试缺少固定装置的遗留代码时,我们有时会使用中间方法。 我们拥有的是应用程序的稍微修改版本,它执行通常的任务,但也可以对功能执行测试。
它不是严格意义上的单元测试,因为您必须假设库的其余部分是正确的,但是,它至少允许您进行比回归/系统测试更多的测试。
Check out the boost serialization library. This will allow you to dump your files to xml which you can then diff for each version.
In order to create your test cases, if you're not already doing so, use gcov to work out the coverage though your functions. This will at least ensure that you've got all statements covered in your function.
Unit Testing
When we're testing legacy code that is missing fixtures, we sometimes use a middle ground approach. What we have is a slightly modified version of our application, which performs the usual tasks, but can also perform tests on functions.
It's not unit testing in the strict sense as you have to assume that the rest of the library is correct, however, it does allow you at least to do more testing than may be possible with regression/system tests.
好的,这里讨论三件事:对遗留代码进行测试、单元测试和验收/回归测试,所有这些都用 C++ 进行。
首先,为了获取遗留代码并对其进行测试,我建议购买 Michael Feathers 撰写的《有效处理遗留代码》的副本。 这是一本很棒的书,将教您任何遗留代码都可以进行单元测试! 我使用了那本书中的技术来对每个人都告诉我无法进行单元测试的东西进行测试,但我还是这么做了
:-)
。其次,对于 C++ 中的单元测试,我刚刚编写了一系列由 5 部分组成的博客文章,详细描述了如何使用 Visual Studio 执行此操作: 使用 Boost.Test 进行 C++ 单元测试。
最后,对于验收/回归测试,我使用 Fitnesse 取得了成功。 这基本上是一个验收测试框架,它使用 wiki 来组织和编写测试。 遍历并解析 wiki 页面,将其转换为对您编写的测试装置的调用。 然后,测试装置在测试(如 wiki 页面所述)和生产代码之间进行协调。 我使用这种机制对整个应用程序进行端到端的回归测试。 将其与您要更改的类的单元测试相结合,这是一个非常强大的错误检测器机制。 回归测试从上面挤压,单元测试从下面挤压,错误被夹在中间! 它对我来说非常有用。
从 fitnesse.org 获取主要的 Fitnesse 发行版 您可以从 sourceforge。 (我是该项目的开发人员。)我们尚未添加主要 Fitnesse wiki 中的 SLIM 支持,但我们确实对多种装置提供了良好的支持。 我们希望尽快添加 SLIM 支持。 我有一个需要完成的基本实现。
OK, there's three things being discussed here: fitting tests onto legacy code, unit testing and acceptance/regression testing, all in C++.
First, for taking legacy code and fitting tests to it, I recommend purchasing a copy of "Working Effectively with Legacy Code" by Michael Feathers. Its an awesome book and will teach you that any legacy code can be unit tested! I've used techniques from that book to fit tests onto things that everyone told me couldn't be unit tested, but I did it anyway
:-)
.Second, for unit testing in C++, I just wrote a 5-part series of blog posts that describe how to do this in detail with Visual Studio: C++ Unit Testing With Boost.Test.
Finally, for acceptance/regression testing I've had success useing Fitnesse. This is basically an acceptance test framework that uses a wiki to organize and write the tests. The wiki page is traversed and parsed to turn into calls into a test fixture that you write. The test fixture then mediates between the test, as described by a wiki page, and your production code. I've used this mechanism to perform regression tests on the whole application end-to-end. Combine that with unit tests for the classes you're changing and its a very powerful bug detector mechanism. The regression tests squeeze from above and the unit tests squeeze from below and the bugs are caught in the middle! Its worked great for me.
Get the main fitnesse distribution from fitnesse.org You can get C++ test runner for FitNesse from sourceforge. (I'm a developer on that project.) We haven't added the SLIM support yet that is in the main fitnesse wiki, but we do have good support for several fixtures. We're hoping to add SLIM support soon. I have a rudimentary implementation that needs to be finished off.