Skip to content

Commit 7ecf111

Browse files
author
Cruz Monrreal
authored
Merge pull request #7819 from lorjala/unittests
Unit testing framework
2 parents 70439dd + 4cf98dd commit 7ecf111

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3934
-1
lines changed

UNITTESTS/.mbedignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

UNITTESTS/CMakeLists.txt

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
cmake_minimum_required(VERSION 3.0.2)
2+
3+
set(PROJECT_NAME unittests)
4+
set(LIB_NAME MbedOS)
5+
6+
project(${PROJECT_NAME})
7+
8+
# Setup c++ standard
9+
macro(use_cxx14)
10+
if (CMAKE_VERSION VERSION_LESS 3.1)
11+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++98")
13+
endif()
14+
else()
15+
set(CMAKE_CXX_STANDARD 98)
16+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
17+
endif()
18+
endmacro()
19+
20+
use_cxx14()
21+
22+
####################
23+
# GTEST
24+
####################
25+
26+
# Download and unpack googletest at configure time
27+
configure_file(googletest-CMakeLists.txt.in googletest-download/CMakeLists.txt)
28+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
29+
RESULT_VARIABLE result
30+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
31+
if (result)
32+
message(FATAL_ERROR "CMake failed for google test: ${result}")
33+
endif()
34+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
35+
RESULT_VARIABLE result
36+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
37+
if (result)
38+
message(FATAL_ERROR "Build failed for google test: ${result}")
39+
endif()
40+
41+
# Prevent overriding the parent project's compiler/linker
42+
# settings on Windows
43+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
44+
45+
# Add googletest directly to our build. This defines
46+
# the gtest and gtest_main targets.
47+
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
48+
${CMAKE_BINARY_DIR}/googletest-build
49+
EXCLUDE_FROM_ALL)
50+
51+
# The gtest/gtest_main/gmock/gmock_main targets carry header search path
52+
# dependencies automatically when using CMake 2.8.11 or
53+
# later.
54+
target_include_directories(gmock_main SYSTEM BEFORE INTERFACE
55+
"${gtest_SOURCE_DIR}/include"
56+
"${gmock_SOURCE_DIR}/include")
57+
58+
####################
59+
# TESTING
60+
####################
61+
62+
enable_testing()
63+
64+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
65+
"${CMAKE_BINARY_DIR}/Testing"
66+
)
67+
68+
####################
69+
# CODE COVERAGE SETUP
70+
####################
71+
72+
if (COVERAGE)
73+
74+
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
75+
message(WARNING "Non-debug build may result misleading code coverage results.")
76+
endif()
77+
78+
# Append coverage compiler flags
79+
set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage")
80+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
81+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
82+
83+
endif(COVERAGE)
84+
85+
####################
86+
# UNIT TESTS
87+
####################
88+
89+
# Set include dirs.
90+
set(unittest-includes-base
91+
"${PROJECT_SOURCE_DIR}/target_h"
92+
"${PROJECT_SOURCE_DIR}/target_h/events"
93+
"${PROJECT_SOURCE_DIR}/stubs"
94+
"${PROJECT_SOURCE_DIR}/.."
95+
"${PROJECT_SOURCE_DIR}/../features"
96+
"${PROJECT_SOURCE_DIR}/../platform"
97+
"${PROJECT_SOURCE_DIR}/../drivers"
98+
"${PROJECT_SOURCE_DIR}/../hal"
99+
"${PROJECT_SOURCE_DIR}/../events"
100+
"${PROJECT_SOURCE_DIR}/../rtos/TARGET_CORTEX"
101+
"${PROJECT_SOURCE_DIR}/../rtos/TARGET_CORTEX/rtx5/Include"
102+
"${PROJECT_SOURCE_DIR}/../cmsis"
103+
)
104+
105+
# Create a list for test suites.
106+
set(TEST_SUITES)
107+
108+
# Get all matched tests.
109+
file(GLOB_RECURSE unittest-file-list
110+
"unittest.cmake"
111+
)
112+
113+
if ("${unittest-file-list}" STREQUAL "")
114+
message(FATAL_ERROR "No tests found. Exiting...")
115+
endif()
116+
117+
# Create unit test targets
118+
foreach(testfile ${unittest-file-list})
119+
####################
120+
# DEFINE TARGETS
121+
####################
122+
123+
# Init file lists.
124+
set(unittest-includes ${unittest-includes-base})
125+
set(unittest-sources)
126+
set(unittest-test-sources)
127+
128+
# Get source files
129+
include("${testfile}")
130+
131+
if(TEST_SUITE_NAME)
132+
set(TEST_SUITES ${TEST_SUITES} ${TEST_SUITE_NAME})
133+
else()
134+
message(FATAL_ERROR "No TEST_SUITE_NAME found in test file. Add it to ${testfile}.")
135+
endif()
136+
137+
set(LIBS_TO_BE_LINKED gmock_main)
138+
139+
# Build directories list
140+
set(BUILD_DIRECTORIES)
141+
142+
if (unittest-sources)
143+
# Create the testable static library.
144+
add_library("${TEST_SUITE_NAME}.${LIB_NAME}" STATIC ${unittest-sources})
145+
target_include_directories("${TEST_SUITE_NAME}.${LIB_NAME}" PRIVATE
146+
${unittest-includes})
147+
set(LIBS_TO_BE_LINKED ${LIBS_TO_BE_LINKED} "${TEST_SUITE_NAME}.${LIB_NAME}")
148+
149+
# Append lib build directory to list
150+
list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.${LIB_NAME}.dir")
151+
endif(unittest-sources)
152+
153+
if (unittest-test-sources)
154+
# Create the executable.
155+
add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
156+
target_include_directories(${TEST_SUITE_NAME} PRIVATE
157+
${unittest-includes})
158+
159+
# Link the executable with the libraries.
160+
target_link_libraries(${TEST_SUITE_NAME} ${LIBS_TO_BE_LINKED})
161+
162+
add_test(NAME "${TEST_SUITE_NAME}UnitTests" COMMAND ${TEST_SUITE_NAME})
163+
164+
# Append test build directory to list
165+
list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.dir")
166+
else()
167+
message(WARNING "No test source files found for ${TEST_SUITE_NAME}.\n")
168+
endif(unittest-test-sources)
169+
endforeach(testfile)

UNITTESTS/CMakeSettings.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "GNU-Debug",
5+
"generator": "Ninja",
6+
"configurationType": "Debug",
7+
"inheritEnvironments": [ "mingw64" ],
8+
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
9+
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
10+
"environments": [
11+
{
12+
"environment": "mingw64"
13+
}
14+
]
15+
},
16+
{
17+
"name": "GNU-Release",
18+
"generator": "Ninja",
19+
"configurationType": "Release",
20+
"inheritEnvironments": [ "mingw64" ],
21+
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
22+
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
23+
"environments": [
24+
{
25+
"environment": "mingw64"
26+
}
27+
]
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)