Unit tests, Platformio, googletest, errors, linking

Hello there,
I’m completely new about unit testing. I’m trying to learn creating them on something easy, so I chose Arduino Uno as experimental device. I followed the tutorial: https://www.youtube.com/watch?v=_xRv-3FWyLE&ab_channel=TomaszTarnowski But I have never been able to complete it due to massive amount of error. First set of error has been solved by installing gcc compiler I used mingw-w64: MinGW-w64 - for 32 and 64 bit Windows - Browse /mingw-w64 at SourceForge.net , but another sets of error appears:

.pio\libdeps\native\googletest\googletest\include/gtest/internal/gtest-port.h:1235:8: error: ‘mutex’ in namespace ‘std’ does not name a type
std::mutex mu_;

These errors came from the version of googletest so I changed the setting in platformio.ini from version 12:

lib_deps = google/googletest@1.12.0

to version 10:

lib_deps = google/googletest@1.10.0

unfortunately, there was still errors while trying to compile program for Arduino, because the compiler cannot compile the google test. I removed the library dependency from platformio.ini:

platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
test_ignore =
test_desktop
test_embedded

[env:native]
platform = native
test_ignore =
test_embedded
lib_compat_mode = off
build_flags = -std=c++11
lib_deps = google/googletest@1.12.0

and change the setting for compiling in the bottom bar from Default (nameOfProject) to env:uno (nameOfProject)
than I have been able to compile program for Arduino And run an empty test
But when I try adding constructor of tested class into TEST method then following error appears:

.pio\build\native\test\test_desktop\LedDiodeTest.o:LedDiodeTest.cpp:(.text+0x13e): undefined reference to LedDiode::LedDiode()' .pio\build\native\test\test_desktop\LedDiodeTest.o:LedDiodeTest.cpp:(.text+0x14a): undefined reference to LedDiode::blick()’
My code:

src/LedDiode.hpp

#ifndef MY_LED_DIODE_H
#define MY_LED_DIODE_H
#include “IBSP.hpp”

class LedDiode
{
public:
LedDiode();
void init(IBSP* hw, uint8_t pin) ;
void setState(uint8_t state) ;
void blick() ;
private:
IBSP* HW;
};

#endif

src/LedDiode.cpp

#include “LedDiode.hpp”

LedDiode::LedDiode(){

}

void LedDiode::init(IBSP* hw, uint8_t pin){
this->HW = hw;
hw->pinDir(pin,IBSP::OUT);
}

void LedDiode::setState(uint8_t state){

}

void LedDiode::blick(){

}

Src/IBSP.hpp

#ifndef IBSP_H
#define IBSP_H
#include <stdint.h>

class IBSP
{
public:
enum{
OUT = (uint8_t)1,
IN = (uint8_t)0
};
virtual void pinDir(uint8_t pin, uint8_t value) = 0;
virtual void setPin(uint8_t pin, uint8_t value) = 0;
virtual uint8_t getPin(uint8_t pin) = 0;
virtual unsigned long now() = 0;
private:
};

#endif

test/LedDiodeTest.cpp

#include “mocks/MockBSP.h”
#include <gtest/gtest.h>
#include “LedDiode.hpp”

TEST(ledDiode, check_pin_while_creating_diode)
{
MockBSP bsp;

//EXPECT_CALL(bsp,pinDir(13,IBSP::OUT));
//EXPECT_CALL(bsp,setPin(13,IBSP::OUT));

LedDiode mydiode;
//mydiode.init(&bsp,13);
mydiode.blick();
EXPECT_TRUE(true);

}

test/test_desktop/mock/MockBSP.h

#include “gmock/gmock.h”
#include “IBSP.hpp”

class MockBSP : public IBSP
{
public:
MOCK_METHOD(void,
pinDir,
(uint8_t pin, uint8_t value),
(override)
);
MOCK_METHOD(void,
setPin,
(uint8_t pin, uint8_t value),
(override)
);
MOCK_METHOD(uint8_t,getPin, (uint8_t pin),(override));
MOCK_METHOD(unsigned long,now,(),(override));
};

I also installed python 3.10.5, because I reed somewhere it is necessary, but nothing changed.
Could you give me an advice please what did I wrong? And also, is it possible somehow use the googletest inside Arduino?
Thank you
Rytirj

Outdated since PIO core 6.0 came out. Unit testing was massively refactored. Please have a look at the new documentation and simliar topics (CI unit test gtest fail: undefined reference to `pthread_setspecific' - #2 by ivankravets, Pio test broke with esp32doit-devkit-v1 in pio core 6.1, File structure to satisfy both building and testing?).

Your particular

error is because the implementation for the component you want to test is in src/ with the src/LedDiode.cpp and src/LedDiode.hpp file. Any sources in src/ are by default not compiled when testing (test_build_src = no by default). So, you should put those two files in lib/LedDiode so that unit tests can find them.

2 Likes

Thank you very much, that helps a lot
BTW for those who are dealing with the errors about mutex (since googletest 12), I figured out that there is nesseseary to have installed gcc with posix I had win32 and test didnt works.

happy testing :wink: