Test Driven Development – Unity + Microchip

ID-10085393Hello!

Today I’ll explain a little bit about unit tests, test driven development (TDD) and how to set up an environment to be able to use TDD concepts in Microchip microcontrollers.

Nowadays there are several approaches in high level software design. Some of them has been adapted to embedded systems, for example, TDD – Test Driven Development. Unlike the conventional approach where all code is first develop and then test and search for bugs is done after the whole development, in TDD we develop the firmware and routine tests at same time, in incremental form. Why and what are the advantages?

  • The whole system tends to be better designed
  • In the end of the project, the code has been already tested
  • Retest entire firmware after new release or changes in source is easy and gives confidence to the developer
  • Avoid pressure from boss to release project as soon as possible, leading to cut time in test/debug step
  • etc

In order to help developers, there are several frameworks, such as Unity. Basically, Unity is a set of macros and functions used to automatise unit tests. I won’t cover in this post how we use Unity, I suggest you to read James Grenning’s book Test Driven Development for Embedded C. Besides Unity, this book introduces the TDD approach and give examples and how to develop embedded systems using TDD technics.

Let’s create a small project to show how configure and how we can use TDD in Microchip microcontrollers, in our case PIC24FJ256GA106. We’ll use a tool called ceedling who will create our environment in order to use Unity. here I’m using Linux Mint and normally I use MPLABx for PICmicro. In the end of the post I’ll leave some references where you can find more details and examples.

1) Inside MPLABx’s project folder (the .X folder), we’ll create a new project with ceedling.

2) In this new folder, we find the project configuration file, project.yml. In our case, we’ll adapt it to use Microchip xc16-gcc compiler and sim30 simulator. The configuration file should be edit as follow:

Note 1: Add compiler’s path to PATH system variable

Note 2: Here the compiler is in /opt/microchip/xc16/v1.11. You have to set it properly in your case

3) Inside pic_unit_test/test folder, create new directory and name it simulation.

4) In pic_unit_test/test/simulation folder, create new file sim_instruction.txt with the following instructions

5)  In pic_unit_test/test/simulation folder, create another file sim_test_fixture.rb with the following instructions

6) Put test code in pic_unit_test/test folder. Production code should be in pic_unit_test/src folder or you can change its path as desired. You can change it in project file in :paths:source and :path:test section.

7) To run all tests

or if you want to run only a specific test, type rake test:test_<test_name>. In my case here, I had test_misc.

run_test

 

 

 

 

 

 

 

 

 

 

 

Here some really useful links:

http://spin.atomicobject.com/2012/10/22/getting-started-with-tdd-for-microchips-pics/

https://github.com/ThrowTheSwitch/CeedlingExample_Microchip

See next time!

Marcelo Jo

Marcelo Jo is an electronics engineer with 10+ years of experience in embedded system, postgraduate in computer networks and masters student in computer vision at Université Laval in Canada. He shares his knowledge in this blog when he is not enjoying his wonderful family – wife and 3 kids. Live couldn’t be better.

LinkedIn 

One Response to 'Test Driven Development – Unity + Microchip'

  1. Roney says:

    nice article!

Leave a Reply