1

在我的模块中有宏之类的功能。这对于模块测试来说不是障碍,但对于包含该模块的其他模块来说却是障碍。

我的问题:有没有办法让 .h 文件的一部分仅对 CMock 可见?

例如:

Module_1.h:

#ifdef MODULE_TEST_CMOCK
    void FunctionLikeMacro_1(unsigned int x);
    unsigned int FunctionLikeMacro_2(void);
#else
    #define FunctionLikeMacro_1(x) (HWREGISTER_1 = (unsigned int)x)
    #define FunctionLikeMacro_2 ((unsigned int)HWREGISTER_2)
#endif

这是我更喜欢的方式。但是我应该在哪里定义 MODULE_TEST_CMOCK?我无法在 Project.yml 中定义它,因为通过此更改,我对 Module_1 的模块测试将失败。但另一方面,在我对 Module_2 的模块测试中,它需要 Module_1.h 的模拟版本,我不能指望 FunctionLikeMacro_1 和 FunctionLikeMacro_2 的调用。

感谢您的时间。:)

4

2 回答 2

0

当 Ceedling 编译和构建您的测试时,它会使用定义的 TEST 集执行此操作,因此您可以编写头文件以在编译测试时更改它们的性质。因此,例如,如果您正在做嵌入式工作并且您通过宏完成所有注册 I/O,那么您可以将其转换为用于单元测试的函数,然后只是模拟它们。

// my_reg_io.h

#ifndef TEST  // production code

#define WRITE_REG(add, val) *(add) = (val)
#define READ_REG(add)  *(add)

#else  // test code
// mockable prototypes
void _my_fake_write(void *add, int val);
int _my_fake_read(void *add);

#define WRITE_REG(add, val) _my_fake_write((add), (val))
#define READ_REG(add)  _my_fake_read(add)

#endif

在读取的情况下,您可以使用存根模拟将行为注入您的代码以测试硬件接口,而无需硬件。超级好用。

于 2020-05-14T20:01:48.457 回答
0

According to docs/CeedlingPacket.md, you can add specific defines to each test file that is to be compiled to the :defines: section in project.yml:

defines: command line defines used in test and release compilation by configured tools

...

<test_name>:

Add preprocessor definitions for specified . For example:

:defines:
  :test_foo_config:
    - FOO_SPECIFIC_FEATURE

So in your case, you would have to add MODULE_TEST_CMOCK for all test files in which want to mock Module_1.h to the :defines: seciton in project.yml.

I tried it using your example header file two test files: test/test_module1.c which included Module_1.h directly and test/test_module2.c which included mock_Module_1.h. I added this to my project.yml:

:defines:
  :test_module2:
    - MODULE_TEST_CMOCK

And this seemed to work fine. I could use FunctionLikeMacro_2_IgnoreAndReturn() from test/test_module2.c and the tests behaved as expected, while the macros were being used directly for the tests in test/test_module1.c.

于 2020-05-10T16:04:24.933 回答