The goal of your unit test should be to test your code at a 100%, but his does not mean writing ALL the possible test cases for every single function, that will be incredible demanding and not really worthy the effort. BUT there is something that at least should be achieved at a 100%, and is all the potentials paths your code could take, you’ll see the code will be full of if/else and loops, we need a new add-on for ceedling

Installing gcovr

$ pip install gcovr

Again let’s move with the most simple example for a better explanation, a single function with an inside if/else sentence, up to now we just tested simple function with arithmetic operations, but now we have a code that can take two different paths, depends on values we passed

app/dummy.c

uint8_t increment( uint8_t flag, uint8_t inc )
{
    /*if flag is equal to 1 the icrement variable by two,
    if different of 2 increment by four*/
    if( flag == 1u ){
        inc += 2u;
    }
    else{
        inc += 4u;
    }
    return inc;
}

we place only the function prototype

app/dummy.h

uint8_t increment( uint8_t flag, uint8_t inc );

And our testing function

test/test_dummy.h

void test__increment__five_by_two( void )
{
    /*first paramter indicate the function under test will
    increment second paramter by two*/
    uint8_t res = increment( 1, 5 );
    TEST_ASSERT_EQUAL_MESSAGE( 7, res, "Increment by two failed" );
}

In project.yml we must add the gcov plugin

:plugins: 
  :load_paths:
    - "#{Ceedling.load_path}"
  :enabled:
    - stdout_pretty_tests_report
    - gcov

And add the gcov configuration

# enable and configure code coverage
:gcov:
  :abort_on_uncovered: true
  :utilities:
    - gcovr
  :reports:
    - HtmlDetailed

Now running your test cases with code coverage using the following command. Looking at the results in our terminal we will see some useful information in the code coverage summary and we can notice only 80% of our code under test have been executed.

$ ceedling gcov:all utils:gcov

Test 'test_dummy.c'
-------------------
Compiling test_dummy_runner.c...
Compiling test_dummy.c...
Compiling unity.c...
Compiling dummy.c with coverage...
Compiling CException.c...
Compiling cmock.c...
Linking test_dummy.exe...
Running test_dummy.exe...
Creating gcov results report(s) in 'Build/ceedling/artifacts/gcov'... Done in 1.252 seconds.

--------------------------
GCOV: OVERALL TEST SUMMARY
--------------------------C:\Users\perez\STM32-v1-workspace\test\Build\ceedling\artifacts\gcov\GcovCoverageResults.html
TESTED:  1
PASSED:  1
FAILED:  0
IGNORED: 0


---------------------------
GCOV: CODE COVERAGE SUMMARY
---------------------------
dummy.c Lines executed:80.00% of 5
dummy.c Branches executed:100.00% of 2
dummy.c Taken at least once:50.00% of 2
dummy.c No calls
dummy.c Lines executed:80.00% of 5

Could not find coverage results for app/app_ints.c
Could not find coverage results for app/app_msps.c
Could not find coverage results for app/main.c
There were files with no coverage results: aborting.

That is not the only thing, GCOV generate a html report for you in the following rute Build\ceedling\artifacts\gcov\GcovCoverageResults.html, you can open it with your explorer

Click in the name of your file to see details. hey is telling you that only test the if but not the else part, also in the column Branch is telling you one out of two branches had been tested ( 1/2 )

Add the missing test case

void test__increment__five_by_two( void )
{
    /*first paramter indicate the function under test will
    increment second paramter by two*/
    uint8_t res = increment( 1, 5 );
    TEST_ASSERT_EQUAL_MESSAGE( 7, res, "Increment by two failed" );
}

void test__increment__five_by_four( void )
{
    /*first paramter indicate the function under test will
    increment second paramter by four*/
    uint8_t res = increment( 2, 5 );
    TEST_ASSERT_EQUAL_MESSAGE( 9, res, "Increment by four failed" );
}

Run again to see in the code coverage report says 100%

$ ceedling gcov:all utils:gcov


Test 'test_dummy.c'
-------------------
Generating runner for test_dummy.c...
...

---------------------------
GCOV: CODE COVERAGE SUMMARY
---------------------------
dummy.c Lines executed:100.00% of 5
dummy.c Branches executed:100.00% of 2
dummy.c Taken at least once:100.00% of 2
dummy.c No calls
dummy.c Lines executed:100.00% of 5

...

Look at the report, all is green!!!

But still there are few thing to tweek, like the files with no code coverage (that are not been tested by the way)

Could not find coverage results for app/app_ints.c
Could not find coverage results for app/app_msps.c
Could not find coverage results for app/main.c

In project.yml put them in the ignore list, and that’s it

# enable and configure code coverage
:gcov:
  :abort_on_uncovered: true
  :utilities:
    - gcovr
  :reports:
    - HtmlDetailed
  :uncovered_ignore_list:
    - app/main.c #
    - app/app_ints.c #
    - app/app_msps.c #