【发布时间】:2014-12-18 17:48:11
【问题描述】:
我一直在寻找答案,但没有找到答案。问题是,我需要为我用 C 编写的程序做一些测试用例。问题是,一些函数接受用户输入,这使我的测试用例等待输入,这不是我想要的。
这是我的一个测试用例:
void test_is_location_free() {
Storage test_storage = new_storage();
Item test_item;
test_storage->inventory[5] = test_item;
test_storage->inventory[5].loc.shelf = 'A';
test_storage->inventory[5].loc.place = 1;
CU_ASSERT(!is_location_free(test_storage, test_item, 'A', 1));
}
这是可行的,因为 is_location_free() 将返回 false,但在函数内部我有另一个函数会不断询问用户新的输入,直到所选位置空闲。
这是它在终端中的样子,它将等待新的用户输入货架:
Suite: HELPER FUNCTIONS
Test: compare_char() ...passed
Test: first_empty_position() ...passed
Test: is_location_free() ...Location not empty, try again!
Shelf:
有什么方法可以忽略所有用户输入,或者定义我的测试用例将使用的未来用户输入?
谢谢!
【问题讨论】:
-
您可以使用
#define TESTING然后#ifdef TESTING ... #else ... #endif将输入替换为准备好的静态数据。 -
是的,可以做到。当涉及到测试用例时,这是一个可以使用的替代方案吗?我只是认为 CUnit 应该对这些类型的情况有某种支持!