问题标签 [cmocka]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c - 为库依赖配置 Cmake 文件
我有一个 C 项目,我正在尝试使用 CMake 进行编译。这是我的项目结构:
该项目需要我正在使用 CMocka 的单元测试,其中引用了main.c
:
我编写了一个 CMake 文件,用于将所有源文件编译到build
目录中,但它无法正常工作:
在上面,我注意到参数顺序是错误的:
/usr/bin/cc -std=c99 -lcmocka -rdynamic CMakeFiles/craking.dir/src/main.c.o -o craking
而我认为应该是:
/usr/bin/cc CMakeFiles/craking.dir/src/main.c.o -std=c99 -lcmocka -rdynamic -o craking
CMake 似乎颠倒了导致链接器出现问题的参数顺序,但我不知道如何解决这个问题。这是我的 CMake 文件:
有任何想法吗?
c - 用 microchip howto 的 xc8 编译器编译 cmocka?
下午好,开发人员,
当我检查最新的 cmocka 版本并尝试使用 xc8 编译器版本 1.41 编译 cmocka 时,代码不会编译语法错误。
不推荐或更改此 cmocka 源的解决方案,因为将来会出现新的 cmocka 版本。不得不再次更改该文件并不好。
所以要求是一个不改变cmocka代码的解决方案。
或者这是完全错误的想法,如果你们的开发人员能推动我朝着这个方向前进,那就太好了。
更新:
消息,但是当我删除此代码时,它会再出现 500 个错误。
c - 如何使用 cmocka 库模拟无法修改的第 3 方库中的函数?
我正在尝试使用 cmocka 库在 c 中编写一个测试用例。我的测试用例正在测试一个函数,该函数然后在内部调用第 3 方库中的函数(无法修改库)。当应用程序未启动并运行时,此函数返回 NULL 值,所以我想模拟这个第 3 方库函数的返回值。我怎样才能做到这一点?
我曾尝试使用 cmocka 的 will_return 函数来获取所需的返回值,但它不起作用
我得到了third_party_func()的多个定义的编译错误。如何处理这种情况?
我想获得所需的值作为我的第三方函数的返回值。
c - cmocka.h:虽然安装了 cmocka,但没有这样的文件或目录——IBM watson 嵌入式 C 客户端库
我正在尝试为 IBM watson 编译嵌入式 C 客户端库示例。
我指的是https://github.com/ibm-watson-iot/iot-embeddedc以获取说明。
编译时出现cmocka.h
未找到的错误。cmocka.h
我手动将文件复制到/home/amruta/iot-embeddedc/test
但仍然出现相同的错误。
cmocka.h 的路径:
/home/amruta/cmocka-1.1.0/include/cmocka.h
test_gatewayclient.c 的路径:
构建路径:
/home/amruta/iot-embeddedc/build
错误 :
c - 我怎么能模拟 void 类型函数?
我有一个 void insert 函数,我想测试它是否正确插入。我正在使用 CMocka 框架进行测试。
我试图dpl_insert(ret, "mock_value")
代替,will_return()
但似乎没有值附加到列表中。
dpl_insert(ret, "mock_value")
打印 0 ,fprintf()
就像没有添加元素一样。
我的目标是从fprintf()
.
c - 我可以使用 cmocka 模拟返回结构的函数吗?
我有一个包含两个数据成员的简单结构。
我有一个按值返回结构的函数。
那么,如何get_my_struct()
使用 cmocka 进行模拟呢?
我试过了
但我得到编译错误。
我阅读了模拟对象的 cmocka 文档,但它没有给我一个明确的答案。
c - Cmocka 链接 apache 运行时函数
我正在尝试使用 Cmocka 对 Apache 模块进行单元测试。
当我运行简单的测试时,我得到一个错误,它找不到函数ap_rprintf
。我尝试像这样构建:
我在https://github.com/tvlooy/cmocka_apache_module/将问题隔离为一个非常简单的案例
少了什么东西?
c - CMake and CMocka standard assertions for compiling tests
I have a small static library project that I'm rewriting from building with Makefiles to modern CMake, which I am trying to learn. My project uses assertions quite heavily for checking preconditions, so I have written a very simple custom assertion macro that conditionally expands into a function that prints formatted diagnostics and then aborts if the library was compiled in debug mode, or expands to nothing if the library was compiled in release mode.
However, I want to be able to test whether these assertions fire correctly. Cmocka allows you to test this by calling mock_assert
in library code, which cmocka will intercept as part of testing. To this end, I want to have another macro, say LIBRARY_TESTING
, that will redefine my custom assertion macro to invoke mock_assert
instead of my own assertion function, so assertions can be tested. The final assertion macro can be considered morally equivalent to the following:
I have been able to get the desired behaviour for building in debug mode (where ASSERT
expands to a call to emit_assertion
) and release mode (where ASSERT
expands into nothing, as desired) through the following Cmake snippet in src/CMakeLists.txt
):
From which building with either -DCMAKE_BUILD_TYPE=Debug
or -DCMAKE_BUILD_TYPE=Release
produces the intended behaviour. Extrapolating how CONFIG
works here, I also added a generator expression that checks Testing
, which defines BASIC_TESTING
when compiling. All is well so far.
I start to run into problems when executing unit tests. For the purposes of exposition, the function that I want to test is equivalent to this, defined in include/example.h
:
With a corresponding unit test in tests/example.c
:
And the contents of test/CMakeLists.txt
:
Now, in order to unit test my library, I want my custom assertions to expand to mock_assert
, so I compile my library for testing (as I understand it):
Everything builds correctly and I have my static library liblibrary.a
where I expect it to be. Additionally, my test executable example
also compiles and links successfully, but when I run it, the test fails with a segmentation fault as if my custom assertion was never called (and the function attempts to dereference the NULL
pointer I intentionally gave it to trigger the assertion). I am reasonably confident that there were no issues linking with cmocka itself, because running the test results in cmocka's fancy command-line output formatting.
In my original makefile-oriented build, all test executable targets would compile a special "testing" library target, and the test executables link to this library target and all assertions are correctly intercepted by cmocka as I would expect. However, in this case, it appears as if the static library I compiled is behaving as if neither LIBRARY_DEBUG
or LIBRARY_TESTING
were defined -- as evidenced by the segmentation fault.
I'm very new to modern cmake, so I feel like I am misunderstanding something conceptual. My question is:
How can I ensure that my static library is compiled with a particular (set of) compilation option(s) (here it is -DBASIC_TESTING
) to ensure that the custom assertions that it fires can be tested with cmocka?
cmake - 如何使用 cmake 处理 Yocto 配方中的符号链接?
我正在尝试将 cmocka 放入嵌入式 Linux 目标板。
我使用 Yocto 创建了 rootfs,并为 cmocka-1.1.5 添加了自定义配方。cmocka 共享库是使用我的自定义 bitbake 配方生成的,但是当我尝试使用bitbake core-image-sato
它构建完整图像时失败并出现以下错误:
我的自定义 bitbake 配方cmocka-1.1.5.bb
如下所示:
谁能让我知道我错过了什么或我做错了什么?
c - 使用 CMocka 测试创作
一点背景知识:我希望对我的代码进行单元测试,该代码内置到共享对象文件中,例如libabc
. 此代码还调用libjson-c
和libcurl
函数。
我计划为产品代码中的每个函数编写函数式测试,模拟从 和 对其他函数libabc
的libjson-c
调用libcurl
。现在我无法a.c
在单个测试文件中安排给定文件的所有测试,因为在每个测试中我都需要从a.c
.
此外,在单个测试中,我想模拟所有对 的函数调用libjson-c
,libcurl
这样我就不必链接 ( gcc -lcurl -ljson-c
) 依赖项,这需要为从 调用的所有函数编写__wrap
函数a.c
。但是由于每个测试用例都希望在包装的函数中出现一些特定的代码,所以我不能一劳永逸地包装它们。
关于如何编写和安排 CMocka 测试的任何想法?我应该创建一个单独的测试文件,该文件在可执行文件中编译以测试产品代码功能吗?