Search code examples
unit-testingcmakex86-64cortex-mctest

CMake UnitTest on x86 in ARM project


I run a Cortex-M Project (ARM). Here, I would like to use the CMake feature add_test where I then can go ahead and call make test.

This works just fine.

The issue I'm having is that the unit test I've added shall run on my PC (x86) not on ARM, therefore, the test fails.

My subproject is:

project(dummy_math_lib_2)

add_library(${PROJECT_NAME}
        src/dummy_math_lib_2.cpp )
        
add_test(${PROJECT_NAME}
    src/dummy_math_lib_2.cpp
    unittest/src/unittest.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC inc)

target_link_libraries(${PROJECT_NAME})

In the main CMakeLists.txt file, I enable "ENABLE_TESTING()" then I do the following:

cmake ..
make test

The sub project tree looks as follows:

.
├── CMakeLists.txt
├── inc
│   └── dummy_math_lib_2.hpp
├── src
│   └── dummy_math_lib_2.cpp
└── unittest
    ├── inc
    └── src
        └── unittest.cpp

EDIT:

project(dummy_math_lib_2)

add_library(${PROJECT_NAME}
        src/dummy_math_lib_2.cpp )
        
target_include_directories(${PROJECT_NAME} PUBLIC inc)

add_executable(UnitTest1 
    unittest/src/test.cpp
)

add_test(Test1 UnitTest1)

output:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/linux/workspace/cmake_t000D_cmake_DK/BUILD
Running tests...
Test project /home/linux/workspace/cmake_t000D_cmake_DK/BUILD
    Start 1: Test1
Could not find executable UnitTest1
Looked in the following places:
UnitTest1
UnitTest1
Release/UnitTest1
Release/UnitTest1
Debug/UnitTest1
Debug/UnitTest1
MinSizeRel/UnitTest1
MinSizeRel/UnitTest1
RelWithDebInfo/UnitTest1
RelWithDebInfo/UnitTest1
Deployment/UnitTest1
Deployment/UnitTest1
Development/UnitTest1
Development/UnitTest1
Unable to find executable: UnitTest1
1/1 Test #1: Test1 ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - Test1 (Not Run)
Errors while running CTest
make: *** [Makefile:104: test] Error 8

EDIT2

Ok i found the issue. The solution is the following:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
make # <--- FIRST BUILD IT!
make test <--- THEN RUN THE TEST

This in theory ;-) Now I have to figure out how I can build this for x86 instead of ARM. But so far ok, thanks everyone helping me out.


Solution

  • The add_test() command should not typically contain source files. It should simply call the executable, with any necessary command line arguments.

    You first need to use add_executable() to specify the actual unit test executable, and you can reference this target in add_test:

    add_library(${PROJECT_NAME}
        src/dummy_math_lib_2.cpp
    )
            
    target_include_directories(${PROJECT_NAME} PUBLIC inc)
    
    # Define the test executable, UnitTest1.
    add_executable(UnitTest1 
        unittest/src/unittest.cpp
    )
    
    # You may want to link the library under test to your test executable.
    target_link_libraries(UnitTest1 PRIVATE ${PROJECT_NAME})
    
    # Add the CTest unit test.
    add_test(Test1 UnitTest1)
    
    # Don't need this. As written, it does nothing.
    target_link_libraries(${PROJECT_NAME})
    

    I highly encourage you to read through the add_test documentation to understand more of the details and different options that this command provides.