How to Unit-Test?

My code could use some unit testing. (do tests before flashing to core)

But as I am new to C++, I don’t know the best way to do that. Does the core-firmware have tests?

Can someone give an example, how I can test my code?

Thanks :sunny:

1 Like

The core-communication-lib uses a lot of unit testing: https://github.com/spark/core-communication-lib You can go through the code to get an idea.

@zachary will be in a better position to offer further inputs here.

1 Like

Also:

3 Likes

Any C++ testing framework should be fine. In the communication library, we’re using UnitTest++. Setup your repo as we do there, with a tests directory containing:

  • the UnitTest++ source folder, and
  • a test runner like Main.cpp (you could just copy that file exactly)

Then you write your test suites in files named TestSomething.cpp, TestAnotherThing.cpp, etc., with #include "UnitTest++.h" at the top.

We run the tests with make test in the root folder of the repo. Be aware that this is building the code for and running it on your own machine. Normally for the Core, we use the makefile in the build folder, which cross compiles the library for the ARM chip.

TDD! TDD! TDD! :thumbsup:

3 Likes

Just came by this via google, and wanted to add that there’s a unit-test library to run unit test code on the core. Just updated the library to use the LED to display test health - so now you get the green bar on the spark core!

Although the original question was for how to unit test before flashing, it’s a good deal easier to write tests on the core, unless you are able to define abstractions to isolate your code from the firmware. For an example of this, please see my flashee library, which has both on-device tests, and a gcc compiled test suite (run before code is flashed to the device).

2 Likes

Hi,

I know it’s not exactly what you want, but just in case you are still looking or just curious.

I wrote about one way of system testing projects based in Particles here:

Verify your projects behave as intended with a telecom grade test tool

1 Like

Hi,

I’m trying to set up unit testing for an application we are creating, but I cannot figure out how to compile it locally. The code is too closely coupled and depends on various spark libraries that are a part of the cloud compilation process. I am currently using Particle Dev to compile, and have been trying to set up the testing in Visual Studio. Do you have any tips on frameworks or change in workflow I could use to do unit tests?

Unfortunately I’m quite new to particle and unit testing. I use Windows 10, and the unit testing framework I have checked out so far is GoogleTest.

Thanks for the help!

Did you find out how to do it?

Hi!

I am also trying to setup some unit tests and have been scouring through the user/test setup. However, I’m still not sure how to mock application.h (millis(), USARTSerial calls, in my understanding). I’m certain you guys have already done it, but what am I missing?

Thank you!

I know this thread has been silent for a while, and for obvious reasons: many embedded developers may not use TDD (Test Driven Development) or unit testing so much, and I plead guilty too.

Coincidentally, I just bought last week-end the book referred by @zach above and have started thinking to implement TDD in my current and future Particle (and others) embedded developments.

This said, I’m a bit skeptical at this point, because particle compile CLI command does not really compile in local project folder, but rather under some subdirectory under ~/.particle. What would be the best way for me to implement AND run my unit tests under my current project folder (tests subfolder e.g.)?

Any clue appreciated. I’m a bit tired to have to always spend hours troubleshooting bugs afterwards and seriously think TDD is the way to go.

I’d go for (real) local building (e.g. via Particle Workbench or another local toolchain).

I know. I thought about that too. But not totally sure how to interact with the particle compilation in Visual Studio (Particle Workbench)…

As far as using the local toolchain, I’ve found this thread: Local build problem: No sources found in but I’m still having some issues to fix… still one step closer probably…

It’s somehow amazing that most people don’t build tests for embedded development, but I guess if the platform was motivating users to do that (providing tools or directions) it would really improve the quality of all devices running on it. Well… I suppose debugging also pays, and possibly even more than the initial coding… that might be the reason the community is not so interested…

I have two examples in our Brewblox codebase using catch test:

Has anyone used VSC Test Explorer and CppUTest? I am reading the book recommended earlier in this thread and I am trying to setup Workbench to use these extensions but I am not sure what to do next having installed Test Explorer UI and the CppUTest test adaptor? Any guidance gratefully received.

I have managed to clone the git repo for CppUTest but could do with some guidance on the following setup: @m_m ? Is this something that could be built into the Workbench (hint hint)?

Using CppUTest with Visual Studio

You can build CppUTest using cmake or in the Visual Studio IDE.

from Visual Studio IDE

Depending on your VS version double click either

  • CppUTest_VS201x.sln - for VS 2010 and later
  • CppUTest.sln - for pre VS 2010

Say yes to suggested conversions. Select the menu item corresponding to run without debugging. CppUTest should build (probably with warnings). When the build completes the test runner runs. You should see over 1000 tests passing and no test failures. The build also produced a static library (cpputest/lib) holding CppUTest you can link your tests to.

To use CppUTest, define an environment variable CPPUTEST_HOME that points to the home directory of CppUTest. You will find a working example and some more help in cpputest-starter-project for Visual Studio.

1 Like

Just thought I would add here that there is an ArduinoUnit https://github.com/mmurdoch/arduinounit not sure how good it is but surprising to me that Particle haven’t ported this or done something similar?

I am also just making my way through the recommended ‘Test-Driven Development for Embedded C’. I am on windows 11, I have installed cpputest-3.8 via cygwin.

If anyone is using cpputest out there, I would love a few pointers on how to create and execute my first test, and how to integrate it with my current particle firmware.

@StngBo
I haven’t put any effort into this since post on April 20. The real constraint at the moment is the small amount of flash space available for programs and thus automating the unit testing is a challenge with anything other than a simple sketch. I did a few tests with the Arduino Test and that supports quite a few assertions. Also, I did have an exchange with developer relations as an idea for a tutorial - not sure whether that has progressed.

I’m hoping most of my tests will live and execute externally from my particle device where possible. I could be sorely mistaken, but I believe this is how cpputest works. So I would test the code while still in the particle workbench, and then just the particle code gets flashed to device when desired.

Is anyone using a testing framework like gtest or above mentioned cpputest? I would love to see a barebones example of how to setup the structure.

Not sure how such tests can be run externally since there is no emulator for the particle devices in the workbench. And testing using an emulator would be impossible for certain types of tests or rather you would need to have intelligent stubs which would also have to be tested thoroughly! Functional/Regression tests built into the source code will become a reality with P2/Photon2 - one of the things I am looking forward to. In terms of not including test framework code when finally compiled for flashing - compile flags can be used for this.

Hey, I'm looking forward to this too. How is this becoming a reality on the Photon 2? Is it because of the larger memory or something else I missed?
Thanks