0

i'm trying to setup CppUTest project with CMake. I'm new to CMake so i started with this example: link

It works fine. Project compiles and i see tests ran.

Then i tried to create my own project based on this example. My project also compiles without error but i always get following output: OK (0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)

I've no idea why CppUTest framework 'doesn't see' tests that i've created.

What i've done is: Created project structure like this:

.unit_tests_prj
|--project
|--code
|    |-CMakeLists.txt
|    |-Tests
|    |    |-CMakeLists.txt
|    |    |-RunAllTests.cpp
|    |    |-leds_tests
|    |    |      |-leds_tests.cpp

The CMakeLists.txt file in code directory contains following code:

cmake_minimum_required(VERSION 2.8.7)
project(rgb_lamp_unit_tests)

# Check if env variable for CppUTest dir is valid
if(DEFINED ENV{CPP_UTEST_HOME})
    message("Found CppUTest home dir at: $ENV{CPP_UTEST_HOME}")
else()
    message("Not found CppUTest directory... Finish build!")
    return()
endif()

include_directories($ENV{CPP_UTEST_HOME}/include)
# set c & cpp lang
enable_language(C)
enable_language(CXX)

# version number
set(rgb_lamp_unit_tests_VER_MAJOR 1)
set(rgb_lamp_unit_tests_VER_MINOR 0)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

# add sources subdirectories for cmake
add_subdirectory(Tests)

CMakeLists.txt in code/Tests dir contains:

project(rgb_lamp_unit_tests)

# add include dirs for CppUTestHome
include_directories($ENV{CPP_UTEST_HOME}/include)

# add cpputest framework library
add_library(cpputest_lib STATIC IMPORTED)
set_property(TARGET cpputest_lib PROPERTY IMPORTED_LOCATION $ENV{CPP_UTEST_HOME}/lib/libCppUTest.a)

# build static library from tests modules
add_library(LedDriverTest ./leds_tests/leds_tests.cpp)

# build executable and link all necessary libs
add_executable(RunAllTests RunAllTests.cpp)
target_link_libraries(RunAllTests cpputest_lib LedDriverTest)

leds_tests.cpp contains:

#include "CppUTest/TestHarness.h"   // include cppUtest framework

TEST_GROUP(LedDriver)
{

  uint16_t virtualLeds;
 
  void setup()
  {
    virtualLeds = 0;
  }

  void teardown() 
  {
  }
};

TEST(LedDriver, LedsAreOffAfterCreate)
{
    FAIL("This should fail!");
} 

I also noticed that when i changed content of leds_test.cpp to:

#include <iostream>
void print_something(){
  std::cout << "some text" << std::endl;
}

and in RunAllTests.cpp did:

extern void print_something();
int main(int argc, char** argv){
  print_something();
  //CommandLineTestRunner::RunAllTests(argc, argv);
}

Then i got "some text" on console output. So the LedDriverTest.a is correctly linked to RunAllTests executable.

I'll be grateful if someone could help me with that, as i want introduce unit testing and tdd in my work.

regards, mf

Mati
  • 1
  • There seems a restriction tests not being discovered, if linked as library, at least according to this site: http://cpputest.github.io/manual.html#writing-your-main I'm not familiar with this framework, but a) I prefer my targets to contain more logic than simply the main and b) you may be able to use an object library to seperate the tests and the main in a similar manner as done in you project – fabian Feb 17 '21 at 22:33
  • I used macro IMPORT_TEST_GROUP(group) and it worked. But i'm wondering why in example, that i linked up tests was discovered and in my project was not, as they are build up with the same manner. – Mati Feb 18 '21 at 17:32

0 Answers0