Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通用单元测试:可以模拟自功能吗?(ceedling/CMock)_C_Unit Testing_Cmock_Ceedling - Fatal编程技术网

通用单元测试:可以模拟自功能吗?(ceedling/CMock)

通用单元测试:可以模拟自功能吗?(ceedling/CMock),c,unit-testing,cmock,ceedling,C,Unit Testing,Cmock,Ceedling,是否可以模拟要测试的文件的功能 例如,我想测试包含以下函数的文件self_test.c: #include "self_test.h" uint8_t function_1(uint8_t argument) { return function_2(argument); } uint8_t function_2(uint8_t argument) { return (argument+1); } 测试文件主要如下所示: #include "mock_self_test.h"

是否可以模拟要测试的文件的功能

例如,我想测试包含以下函数的文件self_test.c:

#include "self_test.h"

uint8_t function_1(uint8_t argument)
{
    return function_2(argument);
}

uint8_t function_2(uint8_t argument)
{
    return (argument+1);
}
测试文件主要如下所示:

#include "mock_self_test.h"

void test_function_1(void)
{
    uint8_t input_value = 8;
    stest_function_2_ExpectAndReturn(input_value, 10);
    uint8_t output_value = function_1(input_value);
    TEST_ASSERT_EQUAL_UINT8(10, output_value);
}
完成self_test.h文件:

uint8_t function_1(uint8_t argument);
uint8_t function_2(uint8_t argument);
当我这样做时,编译器返回:error:Function\u 1。打电话的次数比预期的多

我认为这是一种不好的做法,可能不起作用,但由于我的函数_2相当大,这可以节省我大量的工作,因为我可以独立于函数_2测试函数_1。 我正在处理遗留代码,所以用更好的测试接口重写所有内容不幸不是一个选项

Ceedling输出:

[==========] Running 1 tests from 1 test cases.
[----------] Global test environment set-up.
[----------] 1 tests from test_self_test.c
[ RUN      ] test_self_test.c.test_function_1
test_self_test.c(22): error: Function function_1.  Called more times than expected.
 Actual:   FALSE
 Expected: TRUE
[  FAILED  ] test_self_test.c.test_function_1 (0 ms)
[----------] 1 tests from test_self_test.c (0 ms total)

不,不能模拟从同一编译单元调用的函数。大多数编译器不会在生成的机器代码中引用被调用函数的符号,而是直接将可重定位地址或偏移量放入其中。如果可能的话,他们甚至可以优化呼叫

您可以将源文件剪切成更小的文件,这也是一项繁重的工作。您可以尝试通过脚本自动执行此操作

显然,要测试的项目的软件设计很糟糕-