有你吗,你怎么做 C++自动测试?
就自动测试而言,如何对C++程序进行自动测试?有没有可以用来进行单元测试和集成测试的自动测试框架?
As far as autotest is concerned, how do you do autotest for C++ programs? are there any autotest framework that can be utilized to do unit test and integration test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是在谈论自动测试 Ruby 自动测试吗?如果是这样,也许 Watchr 适合你。是的,您需要在开发计算机上安装 Ruby 运行时,但当文件系统发生更改时,它似乎可以触发几乎可以在命令行上完成的任何操作。例如,如果您希望 Watchr 在源代码树中的 .c/.cpp/.h/.hpp 文件发生更改时构建并运行您的 C++ 测试,您可以执行如下操作
:构建过程已设置(显然您使用的是 Windows),但这应该是它的要点。我们的团队使用构建后事件配置我们的单元测试项目,该事件会自动运行构建的单元测试二进制文件,因此我们可以在
buildAndRunTests.bat
脚本中触发构建过程的该部分并打印它将结果发送到命令行。它可能需要一些调整,但看起来 Watchr 可能是一个不错的选择。当我尝试时(希望下周初),我会更新此回复。更新:我刚刚在我的一个 C# 项目中尝试了这一点,并让它在那里工作。所以我理论上它也应该适用于 C++ 项目。
autotest.watchr:
注意正则表达式末尾的$。这很重要,因为在构建时源树中生成了很多工件,如果其中任何一个与字符串 .cs 匹配,它将触发另一次运行,从而有效地导致无限循环。可以想象,如果您在构建时生成/修改任何源文件,也会发生同样的事情,因此您可能必须找到一种补偿方法。
buildAndRunTests.bat:
然后,在单独的控制台窗口中导航到您的项目目录并运行以下命令(假设autotest.watchr位于项目树的顶部,见下文):
现在,当源树中的任何 .cs 文件发生更改时,它将自动运行 buildAndRunTests.bat 脚本。这只是我本地计算机上的一个示例,因此它可能无法在您的计算机上逐字工作,但您应该能够根据您的需要对其进行调整。
这是供参考的目录结构:
希望对您有所帮助。
Are you talking Autotest ala Ruby Autotest? If so, maybe Watchr would work for you. Yes, you would need to install the Ruby runtime on your development machine, but it looks like it can trigger pretty much anything that can be done on the command line when the file system changes. For example, if you wanted Watchr to build and run your C++ tests anytime a .c/.cpp/.h/.hpp file in your source tree changed you could do something like this:
This particular command obviously makes some assumptions about how your build process is set up (and obviously that you're on Windows), but that should be the gist of it. Our team configures our unit test projects with a post-build event that automatically runs the built unit test binary, so we can just trigger that part of our build process within the
buildAndRunTests.bat
script and have it print the results to the command-line. It might take some tweaking but it looks like Watchr may be a good choice. I'll update this response when I give it a shot (hopefully early next week).UPDATE: I just tried this with one of my C# projects and got it working there. So I theoretically it should work with C++ projects as well.
autotest.watchr:
Note the $ at the end of the regular expression. This is important because there are a lot of artifacts generated in the source tree at build time and if any of them match the string .cs it will trigger another run, effectively causing an infinite loop. Conceivably the same thing will happen if you generate/modify any source files at build time so you may have to find a way to compensate.
buildAndRunTests.bat:
Then, in a seperate console window just navigate to your project directory and run the following command (assumes autotest.watchr is at the top of your project tree, see below):
Now, when any .cs files change in the source tree it will run the buildAndRunTests.bat script automatically. This is just an example from my local machine so it likely won't work verbatim on yours, but you should be able to tweak it to your needs.
This is the directory structure for reference:
I hope this helps.
您可以使用 NUnit 来实现此目的,但可能还有更好的方法。使用 NUnit,您可以在托管 C++/CLI 中编写测试类,该类调用您的 C++ 代码,该代码可能以非托管方式运行。因此,对于此选项,您的一些 C++ 代码现在只是为了使用 NUnit 而以托管方式运行。人们可能会争论这种方法的“纯粹性”。另一个问题是将调试器附加到 NUnit(当然启用了托管/本机)并尝试以合理的方式单步执行托管 C++/CLI 位。尽管如此,我们的办公室使用 NUnit 进行 C++ 单元和集成测试已经有一段时间了。
刚刚看到@Patrick 关于 CPPUnit 的回答,我必须看看。
You can use NUnit to achieve this, but there may be better ways. With NUnit you are writing test classes in managed C++/CLI which is calling your C++ code, which presumably runs as unmanaged. So for this option, some of your C++ code now runs as managed just for the sake of using NUnit. One may debate the "purity" of this approach. Another problem with this is attaching a debugger to NUnit (of course with both managed/native enabled) and trying to step through the managed C++/CLI bits in a sensible manner. Despite this, our office has been using NUnit for C++ unit and integration testing for a while now.
Just saw @Patrick's answer about CPPUnit, I will have to look at that.
xUnit 系列可用于单元测试。它适用于纯 C++ 代码 (CPPUNIT) 和 .Net 代码 (NUnit)。
The xUnit family can be used for unit tests. It exists for plain C++ code (CPPUNIT) and for .Net code (NUnit).
Boost 有一个测试库可以看看周围还有很多其他的。
Boost have a test library you can have a look at among many others around.
上次我在 Qt 中做一些工作时,我使用了 Qt 的 QTestLib 进行单元测试。它确实很好地满足了我的低保真需求。 http://doc.qt.nokia.com/4.6/qtestlib-manual。 html
Last time when I did some work in Qt, I've used Qt's QTestLib for unit tests. It did work well for my lo-fi needs. http://doc.qt.nokia.com/4.6/qtestlib-manual.html