Dinesh Kumar
Dinesh Kumar

Reputation: 605

How to run c unit testcases using CMOCKA framework?

I have recently started working on a project written in C language. To add unit test cases, I searched for C unit test frameworks and I came across this answer C-unittest-frameworks. So I chose mocka and installed it from installation steps. But I am aware of how to run the written test cases. Can somebody help me to run from Command line ( GCC command) the simple unit case below:

 #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); 
  }

And FYI I am referring cmocka. And please let me what are the dependencies required other than GCC and cmocka. Or any Environment variable needs to set for LD paths etc.

Update on this question, i followed following steps:

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

After all, steps, if in build dir I execute ctest tests/, it is running all the test cases. Now I am stuck on how to follow the same steps for my project and run a dummy unit test case.

Upvotes: 1

Views: 3838

Answers (1)

Dinesh Kumar
Dinesh Kumar

Reputation: 605

Finally, after more research on the internet, I came across this very well written article on how to run unit test cases using cmocka. Here is the link run c unit test cases using cmocka.

I also forked his git repo & modified it accordingly, have a look at git repo also git code.

Also, I tried another framework gtest, have a look gtest example code

Upvotes: 2

Related Questions