How to run c unit testcases using CMOCKA framework? - c

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.

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

Related

How to setup a cmocka example with arm-none-eabi-gcc + cmake?

I am developing firmware for stm32f4xx based systems.
For that I setup a toolchain based on the arm-none-eabi-gcc toolchain form ARM and cmake.
This toolchain works on Ubuntu. I can x-compile and debug(via openocd + eclipse IDE).
Now I like to add do some functional testing for my code. I was checking and it seems that cmocka is a good tool to use for embedded software testing.
I am now looking for an example/template that integrates a test into the cmake build.
let's assume a simple function at myfunc.c
#include "myFunc.h"
int buffer[10];
void myFunc(int i, int val) {
buffer[i] = val;
}
if I got it right I can do a test in a separate c file like "test.c"
#include "myFunc.h"
#include <cmocka.h>
// function success
static void test_myFunc_positive() {
for(int i = 0; i < 10; i++) {
myFunc(i,i);
}
}
static void test_myFunc_outofbounds() {
myFunc(100,44);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_myFunc_positive),
cmocka_unit_test(test_myFunc_outofbounds),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Usually I run
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="cmake/STM32Toolchain.cmake"
My question contains some sub questions:
1.) I installed libcmocka-dev. This is for my host system. Do I need to install cmocka for my arm-none-eabi-gcc compiler?
2.) How to setup cmake to pick the cmocka lib, build the test and run it on the host system? Think my toolchain file needs to be ignored.
Your source code looks pretty fine. Here is a recipe of how you could use cmocka.
I recommend to crosscompile cmocka's source code, too. In fact I doing it in this way:
Add cmocka.c to your sources
Add 'cmocka.h and cmocka_pbc.h and cmocka_private.h to your include directories.
Compile and run your software
PS: I don't know libcmocka-dev. I think this is a precompiled version of cmocka?
PPS: I had some trouble on getting cmocka's output redirected to my serial UART. If you're having the same problems, feel free to ask.

Using header only libraries in biicode

Short:
How do I use header only libraries with biicode?
Medium:
When I try to build a block it includes example directories even though I try to set the dependencies explicitly in the biicode.conf of the published block.
Long:
I'm trying to get the unity framework up and running, using biicode.
Unity is great as a unit testing framework for C because you do not need to compile any libraries. If you do your own mocks, you don't even have to run any scripts - there is just a single .c file to include in your compile and you are golden.
I've published the git repo to my biicode block paulbendixen/Unity and since there is no need for any compilation step beyond the c file that accompanies the header that should be included there is nothing else to do.
However, when I include the file, using #include "paulbendixen/Unity/src/unity.h" I get the error when doing bii cpp:build:
Code.c:2:28: fatal error: ProductionCode.h: No such file or directory
#include "ProductionCode.h"
This is in the examples folder and should therefore not be compiled, when I just want to use the unit testing part. Changing the [dependencies] to include unity.h = unity.c unity_internals.h hasn't helped either.
I'm pretty sure the problem should be resolved in the Unity/biicode.conf, but I haven't been able to find a thorough description of this file anywhere.
The simplicity of the Unity library should make it ideal for a build system such as bii, but it seems quite complex to set up.
If it helps, I've used the simple layout and the -r [github for throwtheswitch] option
It is not that simple. Unity uses Rakefiles to build and run the tests, and they have lots of configuration. What can be done for quickly upload it to biicode is just to ignore the tests and publish just the files. This can be done writing a ignore.bii file with the contents:
docs/*
test/*
examples/*
*test*
Wrt to the biicode.conf, the only configuration necessary are the include paths:
[paths]
src
extras/fixture/src
You can check that the manual definition of dependencies is not necessary, if you run $ bii deps --files *unity.h
With these changes, it is possible to publish it. Nothing to build.
Then, to use it in other projects, I have been able to build simple tests:
#include "unity.h"
void testTrue(void){
TEST_ASSERT(1);
TEST_ASSERT_TRUE(1);
}
int main() {
testTrue();
}
Just adding the following to the biicode.conf of the new project:
[requirements]
diego/unityfork: 0
[includes]
unity.h: diego/unityfork/src
It would probably be much easier to make biicode run and build the tests without ignoring them if it used the more typical CMake configuration instead of Rakefiles

Run Plain C Project in Qt Creator

I created a plain c project in qt creator using
File->New file or Project->Non Qt Projects->Plain C Project
main.c
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
test.pro
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.c
I build the project using Ctrl+B which was successful. But I am not able to run the project with Ctrl+R.
I earlier used to run the following command to compile and run c program
gcc main.c
./a.out
But Now I am not able to get how to run c program in qt creator.
I am new to Qt Creator. Please Help
Have you set the run settings of your project correctly ?
Make sure to set them like this https://doc.qt.io/qtcreator/creator-run-settings.html
edit: updated link for recent QtCreator version
Seems like the working directory or executable path are missing.
Is there any output given like "could not execute ./a.out : No such file or directory" ?

Importing CUnit sources

i'm having a problem to use Unit test in C, i've tried to install CUnit in my computer and after include the lib.
i've followed steeps to install the CUnit:
1 - download the sources
2 - configure it using "./configure --prefix='/lib' "
3 - make (only make)
4 - sudo make install
and this is my test source file, it's not making tests, but i can't compile this, i got this error before "CUnit.h: No such file or directory":
#include "CUnit.h"
#include <stdlib.h>
#include <stdio.h>
int main(){
print("Hello");
return 0;
}
I want to know, how can i install CUnit to use in my test source files!
Thanks in advance.
EDIT
HAHA, i got this!
i just run configure without parameters, like that:
"./configure"
As shown in the code example you should use something like this :
#include <CUnit/CUnit.h>
because every CUnit includes are located in a CUnit subdirectory (in general in /usr/local/include/CUnit)
What about adding -I/lib/include flag to include header files installed in /lib/include/CUnit and -lcunit -L/lib/CUnit/lib for linking with the installed libraries?
gcc test_file_source.c -I/lib/include -lcunit -L/lib/CUnit/lib -o testing

CppUTest not working

I am trying to rewrite some legacy C code and would like to have some tests in place before actually starting the rewrite. FOr this I took a look at CppUTest and tried a sample application consisting of a header file chrtostr.h, an implementation file chrtostr.c and a test file called test_chrtostr.c which contents is listed bellow:
#include <CppUTest/CommandLineTestRunner.h>
#include "chrtostr.h"
TEST_GROUP(chrtostr)
{
}
TEST(chrtostr, test_chrtostr)
{
CHECK_EQUAL(chrtostr('n'), "sfsdfds");
}
int main(int ac, char **av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
And the corresponding Makefile.am:
AUTOMAKE_OPTIONS = foreign
CPPUTEST_HOME = ./cpputest
CFLAGS = -g -Wall -I$(CPPUTEST_HOME)/include
LDFLAGS = -L$(CPPUTEST_HOME)/lib -lCppUTest
bin_PROGRAMS = chrtostr test_chrtostr
chrtostr_SOURCES = chrtostr.c chrtostr.h main.c
test_chrtostr_SOURCES = test_chrtostr.c
The issue is that each time I try to run make I get the following traceback which doesn't actually help me too much: http://pastebin.com/BK9ts3vk
You should probably start by getting one of the demos going. You could see how CppUTest is intended to be used with C. My book, Test-Driven Development for Embedded C, will help you get started too. The first few chapters use a C-Only test harness. Later examples use CppUTest (I'm one of the authors of CppUTest). I also describe the advantages of a C++ test harness for C.
James
p.s. - for more information on CppUTest, look at CppUTest.org
That test driver is written in C++. You'll need to compile that as C++, so rename your file to .cpp and make sure g++ is called to drive the compile/link (rather than gcc).
Unfortunately, the "HelloWorld" example in CppUTest is undocumented and while the Appendix in "Test Driven Development for Embedded C" lists only 11 condition checks, I am finding that there are a lot more undocumented helper functions (all pretty much undocumented). I wouldn't recommend CppuTest unless you are trying to understand the concepts of TDD.
I would look for more of a commercial product or you are going to pick up a lot of bad TDD habits or get really frustrated and just move on.
I was just looking at this again. There were a few problems with your code. C++ errors don't always help clear them up.
I added comment before the things i fixed.
#include "CppUTest/TestHarness.h"
//The test file is c++. YOu have to tell it when you are linking to C code
extern "C"
{
#include "chrtostr.h"
}
//A test group needs to have a ';' after it. Under the hood, this macro
//create a base class for the test cases of the same name
TEST_GROUP(Chrtostr)
{
};
//CHECK_EQUAL uses ==. STRCMP_EQUAL actually compares c-strings
TEST(Chrtostr, wrong)
{
STRCMP_EQUAL(chrtostr('n'), "sfsdfds");
}

Resources