Попытка провести модульное тестирование с использованием ожидающих и ожидаемых. Тест не пройден, потому что check_expected всегда проверяет одно и то же значение. Я думаю, что это должно сбрасывать значения при каждом вызове. Но это не то, что происходит. Вот мой тестовый код:
file test_test. c
int run_test_order(void){
int rc = 0;
char str[200];
for(int i = 0; i < 4; i++) {
snprintf(str, sizeof(str), "test: %04X", i);
printf("%s@%d, check_expected \"%s\", i %d\n", __FUNCTION__, __LINE__, str, i);
check_expected(str);
}
rc = (int)mock();
return rc;
}
void test_cmocka_order(void **state){
char test_str[200];
for(int i = 3; i >= 0; i--) {
snprintf(test_str, sizeof(test_str), "test: %04X", i);
printf("%s@%d, expect_string \"%s\", i %d\n", __FUNCTION__, __LINE__, test_str, i);
expect_string(run_test_order, str, test_str);
will_return(run_test_order, 0);
}
run_test_order();
}
file main. c
{
const struct CMUnitTest some_tests[] = {
cmocka_unit_test(test_cmocka_order),
};
// Select output format
cmocka_set_message_output(CM_OUTPUT_STDOUT);
// run tests
return cmocka_run_group_tests(pdm_generator_tests, NULL, NULL);
}
Выходные данные:
test_cmocka_order@201, expect_string "test: 0003", i 3
test_cmocka_order@201, expect_string "test: 0002", i 2
test_cmocka_order@201, expect_string "test: 0001", i 1
test_cmocka_order@201, expect_string "test: 0000", i 0
run_test_order@183, check_expected "test: 0000", i 0
run_test_order@183, check_expected "test: 0001", i 1
[ ERROR ] --- "test: 0001" != "test: 0000"
.../test_test.c:184: error: Check of parameter str, function run_test_order failed
.../test-test.c:202: note: Expected parameter declared here
[ LINE ] --- .../test_test.c:184: error: Failure
I note that the first time check_expected is called the test passes fine. the second time it fails because it tests against the previous value.
Is there anything else that needs to be done for this to work ?
Another unexpected behaviour is that the expect_string had to be stacked in reverse order. No big deal really but a bit awkward at times.