Unit testing libraries

Hi All,

I’m very new to platformIO and one of my big goals is to ensure that library packages I create will follow the same TDD workflow as any applications. So far, I’ve successfully worked through some of the tutorials and I’m able to do TDD and CI for small applications, which is a huge deal for me. My next goal is to be able to create a library package which follows a similar TDD / CI workflow that I can pull into multiple projects (with confidence that it is well tested of course :slight_smile:). I’m wondering if anyone can help me find resources or get me pointed in the right direction – I think this is something really crucial that the documentation on library creation sort of leaves out!

1 Like

Hi,

We are glad that you like PlatformIO. Within PlatformIO ecosystem the library can be represented as generic PlatformIO Project. Possible structure:

├── lib
│   ├── mylib
│   │   ├── platformio.ini
│   │   ├── src
│   │   └── test
│   └── readme.txt
├── platformio.ini
└── src

The source code of the library is located in project/lib/mylib/src. PlatformIO will automatically build your “library project” as a library and link it with main firmware.

Also, please read these:

1 Like

Hi, and thanks for this answer.

I’ve been struggling lately to create a library (to be added to PlatformIO’s registry) that has its own unit tests.

Here is the project’s structure:

.
├── include
│   └── nmeaparser.h
├── platformio.ini
├── src
│   ├── main.cpp
│   └── nmeaparser.cpp
└── test
    └── tests.cpp

And platformio.ini’s content:

[env:uno]
platform = atmelavr
framework = arduino
board = megaatmega1280

It compiles fine using pio run, but when unit testing using pio test, compiler fails to link against the library, according to error messages:

/var/folders/h8/54cqk_1d64d120d3hbbwn3sr0000gn/T//ccEL91PR.ltrans0.ltrans.o: In function `test_unknown_type()':
<artificial>:(.text+0x980): undefined reference to `NMEAParser::NMEAParser()'
<artificial>:(.text+0x99e): undefined reference to `NMEAParser::dispatch(String)'
<artificial>:(.text+0x9c6): undefined reference to `NMEAParser::dispatch(String)'
/var/folders/h8/54cqk_1d64d120d3hbbwn3sr0000gn/T//ccEL91PR.ltrans0.ltrans.o: In function `test_plsr2451()':
<artificial>:(.text+0xa3a): undefined reference to `NMEAParser::NMEAParser()'
<artificial>:(.text+0xa58): undefined reference to `NMEAParser::dispatch(String)'
/var/folders/h8/54cqk_1d64d120d3hbbwn3sr0000gn/T//ccEL91PR.ltrans0.ltrans.o: In function `test_plsr2452()':
<artificial>:(.text+0xc16): undefined reference to `NMEAParser::NMEAParser()'
<artificial>:(.text+0xc34): undefined reference to `NMEAParser::dispatch(String)'
/var/folders/h8/54cqk_1d64d120d3hbbwn3sr0000gn/T//ccEL91PR.ltrans0.ltrans.o: In function `test_plsr2457()':
<artificial>:(.text+0xdf6): undefined reference to `NMEAParser::NMEAParser()'
<artificial>:(.text+0xe14): undefined reference to `NMEAParser::dispatch(String)'
collect2: error: ld returned 1 exit status
*** [.pio/build/uno/firmware.elf] Error 1

Can someone help me out with this?

Took a while, but I finally found the solution. I removed main.cpp (which was hollow anyway), and added test_build_project_src = yes. Now only pio check works, but that’s what I wanted.

2 Likes