嵌入式操作系统中的C/C++单元测试框架

CxxTestC++语言的一个框架,它的风格类似于JUnit/CppUnit/NUnitCxxTestCppUnit的鼻祖之一有感于CppUnit的一些缺点而重写的一套C/C++单元测试框架,目前在C/C++单元测试领域受欢迎程度排行第二, 仅次于CppUnit(NUnitCppUnit更为流行, 虽然NUnit可用于.net下所有语言的测试, 包括C/C++/C#VB, NUnit可能主要用于C#测试, 故我们将其排除在外). 与已有的替代解决方案相比,它具有如下一些优点:
·
不需要RTTI(运行时间类型信息)
·
不需要成员模板功能。
·
不需要异常处理。
·
测试驱动(类似于CppUnitHelper)可以完全自动生成, 省去了很多例行的手工编程.
·
不需要任何外部函数库(包括内存管理、文件/控制台的输入/输出和图形库等)。
·
它完全是作为一套头文件的集合而进行发布的。
上述这些优点使得CxxTest具有特别的可移植性和可用性。由于CppUnit带有Make文件, 所以只能用在主流的操作系统中, 而应用到非主流操作系统中源代码及Make文件修改的工作量就会很大. CxxTest不带Make文件, 所以也可用于非主流操作系统中. 下面的例子给出了CxxTest用在Palm OS中的详细指南, 用在其它嵌入式操作系统中(例如Windows CE)可能也大同小异吧.

 

Palm OS Unit Testing with CxxTest

Last revision: 19 January 2003

Introduction

This page describes how we modified the CxxTest framework to implement a Palm OS Standard IO application to provide a simple command line style interface for unit testing.

Development Environment
  • CxxTest version 2.8; must use this version
  • PRC-Tools version 2.2; earlier versions may work
  • Palm OS SDK version 5; version 4 should work as well
  • GCC version 2.96 on RedHat 7.2; other versions should work as well
What to Test

We test a skeletal String class. The class is declared in String.h and defined in String.cpp, and the tests for it are in TestString.h.

Getting Started

A Palm Stdio application requires the entry point function to be named SioMain. By default CxxTest generates a standard entry point function named main. So we create a CxxTest template file, StdioTestMain.tpl, with the special function definition.

Including Stdio Functions

Palm's StdIO facility allows the use of standard get and put style functions. These functions are provided by StdIOPalm.h. Unfortunately, this header filename conflicts with CxxTest's runner/printer file designed to use stdio functions, StdioPrinter.h, which uses stdio.h.

To work around this conflict, we copy StdioPrinter.h to PalmStdioPrinter.h and modify it, replacing the name of the IO header file. In addition we must remove the reference to the fflush() function, which is not implemented in StdIOPalm.h.

An alternative solution is to modify CxxTest's StdioPrinter.h. We can add a #define constant to the top of the template file to indicate when the file is being used on Palm OS. Then we can conditionally use the appropriate stdIO header and conditionally remove the use of fflush().

However, the purpose of the runner/printer files like StdioPrinter.h is to allow for various implementations of output. The system is designed to accept new implementations such as ours. The downside is that the onus is now on us to maintain the implementation as CxxTest evolves.

Handling ErrTry/ErrCatch Macros

The PRC-Tools implementation of GCC does not permit throwing a C++ exception, so we use the Palm ErrThrow/ErrTry/ErrCatch macros, which are based on setjmp/longjmp. CxxTest must handle the situation where an ErrThrow generated during a test is not contained within an ErrTry/ErrCatch.

To accomodate this requirement, we modify a CxxTest system file rather than subclass or add a new file. We modify TestSuite.h to use the Palm macros if a #define constant, CXXTEST_HAVE_PALM_EM, is present. Conveniently we can define this constant at the beginning of our CxxTest template file, StdioTestMain.tpl. As an aside, notice the implementation in TestSuite.h uses CxxTest's value traits to display the error code passed to ErrCatch.

An alternative to our explicit definition of the CXXTEST_HAVE_PALM_EM constant is for the test generation script, cxxtestgen.pl/cxxtestgen.py, to define this for us based on the content of our actual unit-test code. This technique is already used by the generation scripts to recognize the use of exceptions. This change would not be difficult in itself, but maintenance of the change would be too much work for us. It's really a question for the CxxTest maintainer.

There is another alternative solution to modifying the TestSuite.h system file: create a PalmTestSuite subclass of TestSuite in our own include file, PalmTestSuite.h. However, this technique requires other changes. We must modify TestRunner.h to include PalmTestSuite.h rather than TestSuite.h. We must modify the cxxtestgen.pl/cxxtestgen.py scripts so the scanLineForSuiteStart function does not specifically look for a class named "TestSuite". And of course we must change our test class, TestString, to subclass PalmTestSuite rather than TestSuite.

Results

So far this implementation works fine. Here are a couple of screenshots.

A successful run

An ErrThrow not caught in test code, but caught by CxxTest.

Futures

We plan to continue developing more unit tests. This activity will further test the integration of Palm OS, especially stdIO, with CxxTest.

 

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值