如何开始为遗留嵌入式 C 应用程序(非常紧密耦合的模块)编写单元测试?

发布于 2024-09-07 02:26:13 字数 318 浏览 3 评论 0原文

我目前正在开发一个代码库,该代码库从未编写过任何单元测试。它是为 16 位嵌入式处理器编写的,我想开始至少为我编写的所有代码添加单元测试,然后将其扩展到代码的其他部分。

我的问题是,我发现应用程序级别的每个模块(.c 文件)似乎与项目中的其他 C 文件紧密耦合。对于任何给定文件,这可能是 2-10 个文件以下的任意位置。

  1. 我如何开始编写单元测试?
  2. 消除这种紧密耦合的最佳/快速/最有效的方法是什么?
  3. 此外,单元测试将在 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.

  1. How do I start to write the unit tests?
  2. What are the best/quick/most efficient ways to remove this tight coupling?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-09-14 02:26:13

关于 #3 - 确保它可以移植到 PC,这是我使用的策略:

首先,检查嵌入代码并将任何“int”或“unsigned long”更改为“int16”
或“uint32”(或您选择的任何约定)。

将您在条件内定义类型的部分包含在嵌入式标头中:

#ifndef CORE_TYPE_DEFINITIONS
#define CORE_TYPE_DEFINITIONS
typedef long int16;
/*...*/
#endif

创建一个“PC_Types.h”文件,该文件为 PC 定义相同的类型。

#ifdef CORE_TYPE_DEFINITIONS
#error "Core Types already defined"
#else
#define CORE_TYPE_DEFINITIONS
typedef short int16;
/*...*/
#endif

在 PC 项目中,为每个嵌入式 c 文件创建一个包装器,其中包含以下内容:

#include "PC_Types.h"
#include "ModuleX.c"  //the file under test

#include "TestHarness.h"   //verification functions

int TestModuleXUnit1(void)
{
   /* setup */
   /* call Unit1(); */
   /* verify post-conditions */
   return result;
}

通过包装每个文件,您可以根据需要使用所有耦合函数。 #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:

#ifndef CORE_TYPE_DEFINITIONS
#define CORE_TYPE_DEFINITIONS
typedef long int16;
/*...*/
#endif

create a "PC_Types.h" file which defines the same types for the PC.

#ifdef CORE_TYPE_DEFINITIONS
#error "Core Types already defined"
#else
#define CORE_TYPE_DEFINITIONS
typedef short int16;
/*...*/
#endif

In the PC project, create a wrapper for each embedded c file, which contains the following:

#include "PC_Types.h"
#include "ModuleX.c"  //the file under test

#include "TestHarness.h"   //verification functions

int TestModuleXUnit1(void)
{
   /* setup */
   /* call Unit1(); */
   /* verify post-conditions */
   return result;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文