Run Unit Test On My Files

122 views
Skip to first unread message

Ahmed Salama

unread,
Oct 16, 2023, 7:54:39 AM10/16/23
to ThrowTheSwitch Forums
Hello,

I want to be able to make unit test for some of my C files. I have installed ruby, Ceedling and cloned the repo. Now what?

  Is there any documentation about how to add my files to the build environment, or do I need something else??

Thanks in advance.

Christian Cortes

unread,
Oct 16, 2023, 2:38:06 PM10/16/23
to ThrowTheSwitch Forums
what is your output when you run ceedling version? 

Christian Cortes

unread,
Oct 16, 2023, 2:38:41 PM10/16/23
to ThrowTheSwitch Forums
do you get this? 
Welcome to Ceedling!
    Ceedling:: 0.31.1
       CMock:: 2.5.4
       Unity:: 2.5.4
  CException:: 1.3.3

Christian Cortes

unread,
Oct 16, 2023, 2:46:53 PM10/16/23
to ThrowTheSwitch Forums

also, yes there is, here is the link https://github.com/ThrowTheSwitch/Ceedling/tree/master/docs
On Monday, October 16, 2023 at 4:54:39 AM UTC-7 Ahmed Salama wrote:

Ahmed Salama

unread,
Oct 17, 2023, 3:23:23 AM10/17/23
to throwth...@googlegroups.com
I am not sure if I got you correctly, What I want is --> I have some C files and I want to make unit tests for them and to be able to check the result and coverage report to see the covered branches and statements .

So, How can I use Unity, CMock and Ceedling ? Do I need them all ?

--
You received this message because you are subscribed to a topic in the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/throwtheswitch/CxJL6gXQMps/unsubscribe.
To unsubscribe from this group and all its topics, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/b8de4852-a8db-4ada-91f1-4194fd72c2efn%40googlegroups.com.

Christian Cortes

unread,
Oct 17, 2023, 1:17:44 PM10/17/23
to throwth...@googlegroups.com
sure thing, the only reason i asked about the output of the command ceedling version is just to verify that ceedling was installed correctly. 

so there is a ceedlling command ceedling new < directoryName> that you can run to create the proper file structure to be able to test your source files. 
This command creates the project.yml file, src directory and test directory. 

if you already have your source files, you can run ceedling new <directoryWithSourceFiles> and ceedling will build around it. There are some assumptions that are made by ceedling though, it expects that the directory where your source files like is labeled src/  if it is named something else, an empty src/ will be created. But this is not needed because all you have to do is look for src in your project.yml file and replace it with the appropriate directory where your source files live. 

you need unity because that holds all the asserts that you need to verify your functions. like TEST_ASSERT_EQUAL(), TEST_ASSERT_FALSE, exct.
cMock is used mocking 3rd party or self created files that are called or used in the source file that you are testing. 
ad ceedling is a build manager that makes your life super easy. 

I hope this helped a little, if not, then I apologize. 

Christian Cortes

unread,
Oct 17, 2023, 2:48:25 PM10/17/23
to ThrowTheSwitch Forums
so now the way you would do this is if your test_myfile.c

#include "unity.h" // not needed but i add it so that i can get intellisense 
#include "myFile.h"

// mocks 
#include "mock_helperFile.h"
#include "mock_i2c.h" 
...

void test_fucntionName_should_doSomething(void)
{
 // do stuff
// call the function you are testing
// assert results
}

then in the same level directory where your project.yml lives run 
ceedling test

Ahmed Salama

unread,
Oct 18, 2023, 2:57:19 AM10/18/23
to throwth...@googlegroups.com
Thanks a lot for your support, I want to know if :

are there paths I need to adjust? 
and where shall I locate my src files? 
and which commands shall I use to compile my files with mocks, run the test and check the coverage ?

Ahmed Salama

unread,
Oct 18, 2023, 9:20:30 AM10/18/23
to throwth...@googlegroups.com
here is an example of what I am trying to do :

I added MyTest folder contains my source files 
image.png

I am trying to build and run it and to have mocks also, I tried the following command but it does not work with any headers with absolute and relative paths 
image.png

I am looking for the simplest way to build, Mock and execute the test. But I found it hard to figure out where to add my files, which commands I need to execute from which directory ??

PS: I have Ruby, Ceedling and I have Unity, Cmock and Ceedling repos 

Christian Cortes

unread,
Oct 18, 2023, 2:28:15 PM10/18/23
to ThrowTheSwitch Forums
seems like you might not have installed anything from i am reading and simply downloaded the repos? 

i would follow this guide here to install ceedling using ruby 

when you install ceedling it will also install unity and cmock
you can double check the install by running ceedling version in your terminal window
~ → ceedling version

Welcome to Ceedling!
       Ceedling:: 0.31.1
       CMock:: 2.5.4
       Unity:: 2.5.4
       CException:: 1.3.3


if this works then I would cd to the location of your examples directory and run ceedling new examples
this will create the directories needed for you to begin

your dir structure should look like 
make_example/
MyTest/
temp_sensor/
src/
test/
project.yml

now you are going to want to open your project.yml file and modify the :source: section 

:paths:
  :test:
    - +:test/**
    - -:test/support
  :source:
    - src/**
  :support:
    - test/support
  :libraries: []

change  -src/** to - MyTest/**

but from here i cannot help you create your tests, but you now need to create test files inside the test directory. 
for example if you had your own math.c file and math.h under MyTest/ , then inside the the 
test/
  you will create a test_math.c file and it would look something like 
#ifdef TEST

#include "unity.h"

#include "math.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_math_NeedToImplement(void)
{
    TEST_IGNORE_MESSAGE("Need to Implement math");
}

#endif // TEST



but if you still don't get it, then all i can say is to invest in the introductory course to help you 


good luck

Ahmed Salama

unread,
Oct 21, 2023, 12:27:18 PM10/21/23
to throwth...@googlegroups.com

Christian you are AWESOME, I love you .


I think everything is working just fine so far, but I have this little problem while executing gcov, However I can see the HTML reports !

attached is my .yml file.


Thanks in advance, I really appreciate it <3
image.png

project.yml
Reply all
Reply to author
Forward
0 new messages