unit-testingcmockceedling

How do I use Ceedling/Unity/Cmock to test embedded Systems


I am new at testing embedded systems with ceedling. I was wondering if there is a good place to start in terms of learning how to use ceedling.

The project that I am working on its an embedded System for a CubeSat Project and we need a way to test our code.


Solution

  • The first thing you should understand is that first and foremost Ceedling or any unit test framework is intended to test your code and by extension your ultimate project. You typically test your code on your PC using a standards based compiler of some sort, not necessarily your cross compiler for your embedded system. You can run ceedling generated tests on a target but you probable don't want to.

    The best book on this subject (not perfect but still darn good) is James W. Grenning's Test Driven Development for Embedded C (Pragmatic Programmers).

    I have a personal GIST on setting up under older Windows here. Mostly notes to myself on getting Ruby and GCC on windows.

    Under Linux Ceedling is super easy to setup and run.

    1. confirm you have gcc available
    2. confirm you have ruby with gem
    3. gem install ceedling
    4. ceedling

    That's it. If you are adapting to a new project you will need to fiddle around with the project.yml file to tell ceedling where your source files and include files are, and where you want your test files to go. This can be challenging on some project structures but if all else fails you just put ever folder by name in your project.yml file (not best but ultimate fallback for crazy messed up projects) There is other learning curve stuff like ceedling test_xxx.c files are sort of magic in that they contain your tests but also act like a linker control file. Every include file in the test_xxx.c file is assumed to have a corresponding translation unit (.c file) with the same base name. Ceedling will search it out and compile it into your test. If you want to exclude that code but need some of the dependencies you change the include file name to include the word mock to automatically mock the interface so foo.h becomes mock_foo.h and will create mock functions for all the declared functions found in the header. If you don't have a header/tranalation_unit name pair, you can tell ceedling to compile and link a file by putting its name in a macro called TEST_FILE. So if you have no foo.h but need foo.c:

    TEST_FILE("foo.c")
    

    There is a bunch of other great resources on the Throw The Switch page. Hope that helps. I have never needed to pull the tool from github directly but if you want to send them patches I suppose that is another way, but they put so much work into making it friction-less to setup and start using, I would start with the ruby method first.

    Side note there are some cool unit testing hooks in the module_generator plugin now to automatically generate stubs for testing which may be very useful during on target tests.