How to Unit-Test?


#1

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:


#2

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.


#3

Also:


#4

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:


#5

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).


#6

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


#7

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!


#8

Did you find out how to do it?


#9

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!