cmocka | elegant unit testing framework for C with support for mock | Mock library
kandi X-RAY | cmocka Summary
kandi X-RAY | cmocka Summary
An elegant unit testing framework for C with support for mock objects.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of cmocka
cmocka Key Features
cmocka Examples and Code Snippets
Community Discussions
Trending Discussions on cmocka
QUESTION
Given the following cmocka test with a static variable in a dependency:
main.c
ANSWER
Answered 2022-Feb-15 at 08:02Yes, if you have a static variable in the tested object, you need to compile separated tests if they expect a "fresh" test object.
I would do this anyway, each test in a separated executable, since you have a Singleton in your test object.
This is a fine example why the Singleton pattern is an anti-pattern today. It is hard to test.
QUESTION
I compile the following main.c
with gcc main.c -Wl,--wrap=foo -lcmocka
:
ANSWER
Answered 2022-Feb-14 at 17:00You need to add the declaration for the foo
function. Currently foo(1, 1, 1, bar);
will use an implicit declaration of int foo()
. Implicit declarations are invalid since C99 so you should be able to turn on more warnings to catch this (-Wimplicit-function-declaration -Werror
).
The implicitly declared function takes the arguments you've given it (which will actually match __wrap_foo
, given that int32_t
is a typedef
for int
) and will return int
, not ExampleStruct
, so your program will have Undefined Behavior.
Example:
QUESTION
#include
#include
#include
#include
#include "stdint.h"
typedef enum {
eSTATE_STARTUP,
eSTATE_ADDRESSING,
eSTATE_RANDOMISE,
eSTATE_SELECTED
}e_State_t;
void test_main()
{
static e_State_t e_state = eSTATE_STARTUP;
if(get_rx_dali_flag())
{
application_process(&e_state);
}
switch(e_state)
{
case eSTATE_STARTUP:
{
set_led_frequency(0);
break;
}
case eSTATE_ADDRESSING:
{
set_led_frequency(1);
break;
}
case eSTATE_RANDOMISE:
{
set_led_frequency(10);
break;
}
case eSTATE_SELECTED:
{
set_led_frequency(100);
break;
}
default:
break;
}
}
int __wrap_get_rx_dali_flag(void)
{
int data_available = mock_type(uint8_t);
return data_available;
}
void __wrap_application_process(e_State_t * ptr_state)
{
check_expected_ptr(ptr_state);
*ptr_state = mock_type(e_State_t);
}
void __wrap_set_led_frequency(uint16_t frequency)
{
assert_int_equal(frequency, 1);
}
void main_should_setLedFreq_1_when_stateADDRESSING()
{
will_return(__wrap_get_rx_dali_flag, 1);
will_return(__wrap_application_process, eSTATE_ADDRESSING);
test_main();
}
int main()
{
const struct CMUnitTest tests[] =
{
cmocka_unit_test(main_should_setLedFreq_1_when_stateADDRESSING),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
...ANSWER
Answered 2021-Nov-20 at 11:54Just use:
QUESTION
I'm trying to mock some functions using cmocka:
...ANSWER
Answered 2021-Jul-02 at 10:59According to your test, it seams that function you are testing is led_status_toggel
. If that is the case, you should not mock it. You should just remove will_return(led_status_toggel,SET);
, since your led_status_toggel
is probably something like this (You dind't share it so I don't know exactly):
QUESTION
I want to use libssh library in Qt and my os is ubuntu 18.04 .
so I cloned the library's file and tried to build it with cmake.
I followed the tutorial in INSTALL folder but the building process can't be completed.
at first I had this error :
ANSWER
Answered 2020-Jul-12 at 06:52
file INSTALL cannot copy file
"/home/heydari.f/libssh-mirror/build/libssh.pc" to
"/usr/local/lib/pkgconfig/libssh.pc"
This is a permissions issue. Use sudo make install
to solve this. sudo
gives root privileges to a command. Use it carefully.
** error: undefined reference to 'ssh_session_is_known_server'**
undefined reference
usually means you are trying to access something, in this case a function, which is not found. This function belongs to libssh, so it means that either you don't have libssh-dev
installed or you are giving the path to the library incorrectly. Since the latter is highly unlikely, i am going to assume libssh
is missing. On Ubuntu you can install it using following command:
QUESTION
I have the following code that works perfectly with gcc
by running the command:
ANSWER
Answered 2020-May-22 at 01:22The include_directories()
command only affects header search paths (stuff that is used through #include
). It has no effect on library search paths.
Since you know the full path of the library, you should just be linking directly against said full path:
QUESTION
So, toy program that duplicates the issues I am having using cmocka for developing unit tests for existing code. The issue is a nested function call does not mock, which makes the unit test dependent on the nested function call performing properly. Please note, that "mockable_static" define was used as the original code has static functions that exist as "internal function calls", but for purposes of unit tests, these are open to external calls. (See stackoverflow post where this idea came from)
Without further ado, here is the code:
func.h:
...ANSWER
Answered 2020-Feb-25 at 23:45Alright, so there are a few different avenues to solve this problem. I am posting the solution so someone else can see this.
Solution #1: separate nested function calls into separate .c files. IE- func.c contains foo() and (newfile)bar.c contains bar(). This allows the GCC --wrap=bar to work within func.c as it needs to link against another file.
Solution #2: build separate tests for testing bar and foo. Use the following line in func.c to make bar "weak"
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cmocka
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page