search the lab

Embedded Testing with Unity & CMock (by Mark)

Embedded Testing with Unity & CMock

James Grenning's book (features Unity)

Test Driven Development for Embedded C


Entries in eclipse (2)


Using with IDEs

Sometimes using tools through a command line just isn't satisfying enough. You want to trigger scripts and tests directly from your favorite editor. Here we're collecting tips and tricks for dealing with some IDEs.

Sublime Text
Emacs - Thanks Martyn!


Using Eclipse IDE

Eclipse has become gained popularity as an IDE for C/C++ development. Some help is provided below on how to use Eclipse for exploring and using CMock and Unity.

Create a Project.

  • Open Eclipse and set workspace one level under the folder where the desired project resides
  • Right-click in the Project Explorer pane and select New -> C Project
  • Type in the name of the subfolder where the project resides in the Project field
  • In the Project Types pane, select the Makefile project folder (not the Hello World C++ Project)
  • In the Toolchain pane, make sure -- Other Toolchain -- is selected and click Next

Add Desired Rake Targets (using the Make Targets pane)

  • Click on the project folder to open it up
  • Right-click on the project folder and select Add Make Target
  • For Target Name and Make Target, enter the text "default"
  • Under Build command, uncheck Use default and set Build command to rake (or rake.cmd on Windows)
  • Click Create
  • Now, you can double-click on the new target to execute it, and the output will be displayed in the Console pane.

You can add additional Rake targets in a similar manner as desired. Here's a list of suggestions:

NameArgumentWhat it's for
targets-Tlists all the possible rake tasks
test deltatest:deltatests just the things that have changed
test thistest:${resource_name}runs the test for the file we are currently editing
create moduleauto\generate_module.rb ${string_prompt:new_module_name:Module}create a module after prompting for its name

Exclude Your Mocks From Auto-Completion and Hover Aids

  • Right-Click your Mock directory and select Exclude From Build
  • Right-Click your project and select Index -> Freshen All Files to stop indexing Mocks

NOTE: This has the advantage of making development aids (like Hover Info) find the REAL version of your files instead of the Mocks. The downside is that you won't get autocompletion suggestions for your Mocks when writing tests.

Make Life Easier by Tracking Ignores

  • Open workspace preferences (Under Eclipse menu for OS X and Linux, Windows menu under Windows)
  • Expand C/C++ -> Task Tags
  • Add Tag TEST_IGNORE to the list. I set mine to high.
  • Click OK
  • Optionally Right-Click your project and select Index -> Freshen All Files to pick up any existing tags
  • Your Task pane will now track all unfinished tests for you.

Code Templates

  • Add new templates at Windows → Preferences → C/C++ → Editor → Templates
test void test_${function_name}_Should_${should}(

ignore void test_${function_name}_Should_BeCompleted(void)
    TEST_IGNORE_MESSAGE("${function_name} Needs Definition.");
  • To use a template, type the start of the template name (for example “try”) then hit your hot key combination for autocomplete (default “Ctrl + Space”). You can then tab between fields and fill them out.

Error Links

  • Install the SunShade? plugin
  • Edit plugin.xml to include the following lines:
  • Add regular expression to Windows → Preferences → SunShade? → ErrorLink? ^\s*(.+\.c):(\d+):test
  • "(.+\.c)",(\d+) (?:Error|Warning)
  • restart eclipse from the command line: eclipse -clean
  • Verify the Test tools are giving you full paths

Hot-Key To Open Source For Test or Test For Source

  • If you have the SunShade? plugin installed, you can also set up a hotkey to automatically open the Test file for the source file you are currently viewing or vice versa.
  • Edit one of the BSF Actions at Windows → Preferences → SunShade? → Scripted Actions
    • Set the Key Name to whatever description you like.
    • Paste this script into one of the actions:
var wb = PlatformUI.getWorkbench(); // IWorkbench API
var editor = // IEditorPart API
if (editor)
  var input = editor.getEditorInput(); // IEditorInput API
  var path = input.getPath(); // IPath API
  var filename = path.toOSString();
  if (filename.indexOf("Test") == -1)
    filename = filename.replace("src\\","test\\Test");
    filename = filename.replace("test\\Test","src\\");
  //alert("Filename: " + filename);
  • Tweak the script if needed. It currently assumes you keep your files organized into src/ and test/ directories
  • Update the hotkey in Windows → Preferences → General → Keys for ‘Bsf User Action #’ if you dislike the default