如何开始为遗留嵌入式 C 应用程序(非常紧密耦合的模块)编写单元测试?
我目前正在开发一个代码库,该代码库从未编写过任何单元测试。它是为 16 位嵌入式处理器编写的,我想开始至少为我编写的所有代码添加单元测试,然后将其扩展到代码的其他部分。
我的问题是,我发现应用程序级别的每个模块(.c 文件)似乎与项目中的其他 C 文件紧密耦合。对于任何给定文件,这可能是 2-10 个文件以下的任意位置。
- 我如何开始编写单元测试?
- 消除这种紧密耦合的最佳/快速/最有效的方法是什么?
- 此外,单元测试将在 PC(32 位)上运行,嵌入式代码适用于 16 位处理器。在将代码移植到 PC 时如何确保这一点得到解决?
I am currently working on a code base, that has never had any unit tests written on it. It has been written for a 16-bit Embedded processor, and I would like to start to add unit tests for all the code that I write, at a minimum and then extend this to other parts of the code.
My problem with this is, I have found that each module (.c file) at the application level, seems to be tightly coupled to other C files in the project. For any given file, this may be anywhere from 2-10 files down.
- How do I start to write the unit tests?
- What are the best/quick/most efficient ways to remove this tight coupling?
- Also the unit tests will be run on the PC, (32 bit) and the embedded code is for a 16-bit processor. How do I ensure this is taken care of when porting the code to the PC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于 #3 - 确保它可以移植到 PC,这是我使用的策略:
首先,检查嵌入代码并将任何“int”或“unsigned long”更改为“int16”
或“uint32”(或您选择的任何约定)。
将您在条件内定义类型的部分包含在嵌入式标头中:
创建一个“PC_Types.h”文件,该文件为 PC 定义相同的类型。
在 PC 项目中,为每个嵌入式 c 文件创建一个包装器,其中包含以下内容:
通过包装每个文件,您可以根据需要使用所有耦合函数。 #include 包装文件中的原始源文件允许您直接从源代码控制系统添加对嵌入式代码的更新,无需任何修改。在包含的源代码之后添加测试函数可以让测试代码完全访问模块的所有函数,即使它们没有公共标头。
Regarding #3 - making sure it is portable to PC, here's the strategy I use:
First, go through the embedded code and change any 'int' or 'unsigned long' to 'int16'
or 'uint32' (or whatever convention you choose).
Wrap the section in the embedded header where you define the types inside a condition:
create a "PC_Types.h" file which defines the same types for the PC.
In the PC project, create a wrapper for each embedded c file, which contains the following:
By wrapping every file, you have all the coupled functions available as needed. #including the original source file in your wrapper file allows you to drop in updates to the embedded code directly from your source control system without any modification. Adding the test functions after the included source gives the test code full access to all the module's functions, even if they don't have a public header.
我首先要重读 Michael Feathers 的《有效处理旧代码》。
I would begin by rereading Michael Feathers' Working Effectively with Legacy Code.