gpt4 book ai didi

c - 如何使用CMOCKA框架运行c单元测试用例?

转载 作者:行者123 更新时间:2023-11-30 16:06:54 29 4
gpt4 key购买 nike

我最近开始从事一个用C语言编写的项目。为了添加单元测试用例,我搜索了 C 单元测试框架,并找到了这个答案 C-unittest-frameworks 。所以我选择了mocka并从installation steps安装它。但我知道如何运行书面测试用例。有人可以帮助我从命令行(GCC 命令)运行下面的简单单元案例:

 #include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h> /* A test case that does nothing and succeeds. */

static void null_test_success(void **state) {
(void) state; /* unused */
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

仅供引用,我指的是cmocka 。请告诉我除了 GCC 和 cmocka 之外还需要哪些依赖项。或者需要为 LD 路径等设置任何环境变量。

更新这个问题,我按照以下步骤操作:

1. git clone https://gitlab.com/cmocka/cmocka.git
2. cd cmocka && mkdir -p build && cd build/
3. cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Debug -DUNIT_TESTING=ON ..
4. make && make install

毕竟,步骤,如果在构建目录中我执行ctest测试/,它正在运行所有测试用例。现在,我陷入了如何对我的项目执行相同步骤并运行虚拟单元测试用例的困境。

最佳答案

最后,经过在互联网上的更多研究,我发现了这篇关于如何使用 cmocka 运行单元测试用例的非常好的文章。这是链接run c unit test cases using cmocka .

我还 fork 了他的 git repo 并进行了相应的修改,也看看 git repo git code .

另外,我尝试了另一个框架gtest,看看gtest example code

关于c - 如何使用CMOCKA框架运行c单元测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59817576/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com