Integrate Cmock with Unity unit testing framework

Hello,

While using the development version of platformio 2.0, I noticed a change in the way unit testing was implemented.

Correct If my understanding is skewed, we are using the platform = native declaration in the platformio.ini file to unit test the code locally using the systems gcc. The other defined environments help is conducting the target testing. Say, if we are using the arduino uno for a project, due to the differences in avr-gcc and native gcc, the tests cannot be written in the same fashion. Arduino expects the setup() and loop() functions and the native gcc expects and main() function. Hence we are seperating the tests into test_native and test_embedded and adding the test_ignore option in both environments in the platformio.ini.

This approach requires that the code architecture itself be independant on the underlying API for the arduino if we are testing it on the native platform. We are limited to testing only the program flow independant of the hardware. There is no way to unit test the hardware itself on the native platform.

I would like to request if we can integrate cmock into the test framework along with unity, so that the underlying arduino API itself can be mocked out and simulated even in the native unit testing. Presently, there is no way we can test if the code performs as expected when a hardware register or timer etc returns a out of bounds value or throws an errror.

1 Like

Hi,

Have you seen this? You can have “fake” library with mocked APIs.

No I haven’t Ivan. Thank you. I ll have a look at it and get back.

I have a a preliminary look at it and it seems sound.
Is there an end to end example which demonstrates all its functions.

@r89m
Hello, I have had a look at your mock lib for the unit testing in the link Ivan posted above. I was wondering if you could help me out in this regard. Do you have a end to end example or documentation that explains the whole usage pattern which will definitely be a big help to me and also helpothet who might have the same requirements.
Thank you.

Hi Krishna,

I don’t have an example showing all aspects of how to use the ArduinoFrameworkFake at the moment, but I have been using it in the following library - GitHub - r89m/Transition: Arduino library for Transitioning between two values over time

The current setup is fairly specific to my machine as I have not yet made the Framework Fake PlatformIO compatible.

As an initial test, try cloning that library into a folder on your computer and also clone GitHub - r89m/ArduinoFrameworkFake to the same directory: ie

dev-folder
  |-ArduinoFrameworkFake
    |-src
  |-Transition
    |-src
    |-tests

Then run pio test -e native

The library I’ve linked to only makes use of the millis() function, although pinMode, digitalRead, digitalWrite, analogRead and analogWrite are already supported. Adding additional methods is fairly trivial (and any pull requets would be greatly received!)

Let me know if you get stuck anywhere.

Richard

Just remembered, FakeIt (which my fake framework makes use of) requires some C++11 features, so you’ll need g++ / gcc version 4.8 or newer.

You can use this single standalone header file. Place it to lib/FakeIt/fakeit.hpp and use as #include <fakeit.hpp>. Does it work?

Yep, that’s what I’m doing at the moment - see ArduinoFrameworkFake/AxxTest.h at master · r89m/ArduinoFrameworkFake · GitHub

1 Like

Cool, maybe we should have standalone “FakeIt” library in our registry?

Works for me. I guess I would then need to PlatoformIO-ise ArduinoFrameworkFake by adding a platformio.ini with a dependency on the FakeIt library?

Yes, lib_deps = FakeIt. Or even to add dependencies to ArduinoFrameworkFake library. I’ll add it later