单元测试嵌入式 C++使用 Visual Studio 2010 编写代码
我有要重构/添加的遗留代码。该代码是用 C++ 编写的,针对使用 Greenhills 编译器的嵌入式设备。我听说Visual Studio 2010有更好的测试框架并且需要更少的工作来编写测试用例。是否可以使用VS2010对嵌入代码进行单元测试?有关如何执行此操作或逐步过程的示例将不胜感激。
如果没有,CppUnit/CxxTest如何与VS2010集成?我找不到关于如何实现这一目标的明确文档。
I have legacy code that I am going to refactor/add. The code is written in C++ and is targeted for embedded device using Greenhills compiler. I heard that Visual Studio 2010 has better testing framework and requires less work to write test cases. Is it possible to use VS2010 to unit test the embedded code? An Example on how to do it or stepwise procedure will be greatly appreciated.
If not, how CppUnit/CxxTest can be integrated with VS2010? I couldn't find clear documentation on how to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 Visual Studio 中编译嵌入式软件。为平台特定功能以及 Green Hills 细节(如果有)创建存根。还要注意任何特定的 Green Hills 宏。您需要找到或创建等效项。
我在 Visual Studio 2008 中使用 CppUnit 和 wxWidgets。我没有尝试单独将 CppUnit 与 Visual Studio 2010 集成。
请记住,您希望将验证代码与嵌入代码分开。嵌入式代码应作为库和头文件导入到您的验证项目中。这使测试保持诚实(尽管我意识到您可能无法将 Green Hills 库与 Visual Studio 库链接起来)。否则,使用 VS 构建嵌入式代码,但在构建步骤中作为单独的库。
如果可能的话,围绕软件需求设计您的测试。之后,设计一些代码来执行公共功能。请记住,开发人员不应该编写代码来满足测试,测试人员也不应该编写代码来满足开发人员的功能。 “如果存在的话,测试一下。”或“如果使用,测试它”。例如,平台可能有 DMA 控制器但不使用它。
编辑 #1 -- 示例
给定“嵌入”或实现中的以下类:
CppUnit 测试类将如下所示:
示例测试方法:
在上面的示例中,测试类创建要测试的类的实例,然后进行练习的方法。关键点是测试类与系统隔离,或者换句话说,测试类不影响被测系统。将测试代码放在单独的位置将有助于强调这个概念。将“嵌入”代码视为只读。测试到极限,坚守阵地,减少的产品退货数量将是你的奖励(并增加公司的利润)。
HTH。
You can compile embedded software in Visual Studio. Create stubs for platform specific functionality and also for Green Hills specifics, if any. Also beware of any specific Green Hills macros. You'll need to find or create equivalents.
I am using CppUnit in Visual Studio 2008 with wxWidgets. I have not tried to integrate CppUnit with Visual Studio 2010 separately.
Keep in mind you want to keep the validation code separate from the embedded code. The embedded code should be imported into your validation project as a library and header files. This keeps the testing honest (although I realize that you may not be able to link a Green Hills library with a Visual Studio library). Otherwise build the embedded code using VS, but as a separate library in the build step.
Design your testing around the Software Requirements, if possible. After that, design some code to exercise the public functions. Remember, the developer's should not write code to satisfy the testing, nor should the testing folks write code to satisfy the developer's functions. "If it exists, test it." or "If it is used, test it". For example, the platform may have a DMA controller but not use it.
Edit #1 -- Example
Given the following class in the "embedded" or implementation:
The CppUnit testing class would look like:
An example testing method:
In the above example, the test class creates an instance of the class to test, then exercises the methods. The key point is that the test class is isolated from the system or put another way, the test class does not influence the system under test. Placing the test code in a separate location will help emphasize this concept. Treat the "embedded" code as read-only. Test to the extremes, hold your ground, the reduced number of product returns will be your reward (and adds to the profit of the company).
HTH.