Unit Tests on Native Platform

I was transitioning an existing platformio v2 project to v3, and attempting to add some tests as I went, and I was only able to include static libraries from the lib folder after changing the name of our main file form our_project_name.ino to main.cpp, and including the library in main as well as in the test. I didn’t see any mention of these sorts of issues in the docs.

Is this an issue, or was I just setting something up incorrectly?
I am on a mac with OSX El Capitan v 10.11.4.

What do you mean under “static” library? *.a?

Code that is included in the ‘lib’ folder in my project.

I had read that when I compile my project they are compiled to static libraries of the form libMyFolder.a and then linked appropriately such that they can be accessed in main via #include <MyFolder.h>.

After figuring out a few more things, I think that I am left with the following questions:

  • Is it expected that if a library is not included in main.cpp, but is included elsewhere, a test file for example, that it should not be compiled?
  • Are linker errors expected if a library contains only a header file?

Do you mean that you need some library for Unit Testing test but don’t use it in project src?

Well the actual case that I ran into is that I had a .ino project file, and then I wanted to write some tests that run locally, because the file wasn’t included in a file called main.cpp it didn’t get compiled and I wasn’t able to write the tests.

If I rename my *.ino file main.cpp then it will work. That just didn’t seem like expected behavior to me though, and took me a while to figure out the problem. This however is problematic if someone ever wants to use the arduino ide to build the project.

Could you provide simple project to reproduce this issue?

Here is a link to a sample project. It will fail to build unless the name of the source file is changed from
main.ino to main.cpp

This is not an issue. You are going to process test on the host machine(PC, not embedded) using Arduino framework. That is impossible.

INO isn’t valid CPP file. PlatformIO converts INO to CPP on-the-fly when option framework = arduino is set.

What is the recommended way to utilize native testing for an Arduino project? Is there an alternative way to ensure that code in the lib folder will be compiled for and available for unit tests on the native platform?

I faced same problem. I want to test my library under lib directory such as lib/sample.
But I could not load and link sample.h from test code under test directory.
The library does not contain Arduino.h.

Sorry, my comment made from my confusion. Please forget it.
I thought there were two problems below, and 2nd one is mine.

  1. Can’t build test without renaming main.ino to main.cpp
  2. Can’t build test even if renamed to main.cpp

2nd one was generated from my mistake.

Ok, let’s me explain in details. There are 2 types of Unit Test in PlatformIO:

  1. Local
  2. Remote.

Local testing means that you can run a test on HOST MACHINE (PC) or on the embedded board.
Remote testing means that you can run unit test on the remote device or machine from anywhere in the world. For example, you have a project that is linked with Continuous Integration System. Using Remote Unit Testing you can configure CI (Travis,Appveyor, etc) to process unit tests on remote board or using PIO Board Farm.

You can write different tests. The core idea of unit testing is to split code into components that could be reused independently from the main “cycle” (firmware). If this component is multiplatform (doesn’t depend on framework, SDK or specific platform), you can use it for both cases: for “localhost” machine testing and for embedded.

The Calculator example demonstrates this functionality.

That is very helpful. Thank you very much!

1 Like