Skip to content

Commit 6c34802

Browse files
authored
Merge pull request #8153 from lorjala/unittests-autoname
Unit tests: automatic unit test suite naming
2 parents 843b15b + 8f340b1 commit 6c34802

File tree

19 files changed

+13
-61
lines changed

19 files changed

+13
-61
lines changed

UNITTESTS/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,17 @@ foreach(testfile ${unittest-file-list})
147147
# Get source files
148148
include("${testfile}")
149149

150-
if(TEST_SUITE_NAME)
151-
set(TEST_SUITES ${TEST_SUITES} ${TEST_SUITE_NAME})
152-
else()
153-
message(FATAL_ERROR "No TEST_SUITE_NAME found in test file. Add it to ${testfile}.")
154-
endif()
150+
get_filename_component(TEST_SUITE_DIR ${testfile} DIRECTORY)
151+
152+
file(RELATIVE_PATH
153+
TEST_SUITE_NAME # output
154+
${PROJECT_SOURCE_DIR} # root
155+
${TEST_SUITE_DIR} #abs dirpath
156+
)
157+
158+
string(REGEX REPLACE "/|\\\\" "-" TEST_SUITE_NAME ${TEST_SUITE_NAME})
159+
160+
set(TEST_SUITES ${TEST_SUITES} ${TEST_SUITE_NAME})
155161

156162
set(LIBS_TO_BE_LINKED gmock_main)
157163

@@ -178,7 +184,7 @@ foreach(testfile ${unittest-file-list})
178184
# Link the executable with the libraries.
179185
target_link_libraries(${TEST_SUITE_NAME} ${LIBS_TO_BE_LINKED})
180186

181-
add_test(NAME "${TEST_SUITE_NAME}UnitTests" COMMAND ${TEST_SUITE_NAME})
187+
add_test(NAME "${TEST_SUITE_NAME}" COMMAND ${TEST_SUITE_NAME})
182188

183189
# Append test build directory to list
184190
list(APPEND BUILD_DIRECTORIES "./CMakeFiles/${TEST_SUITE_NAME}.dir")

UNITTESTS/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ Each class to be tested requires two files for unit testing:
203203

204204
A unit test definition file `unittest.cmake` requires variables to be set for a test to be configured. File source paths in `unittest.cmake` files need to be relative to the unit test folder and `CMakeLists.txt`.
205205

206-
* **TEST_SUITE_NAME** - Identifier for the test suite. Use naming convention *PATH_TO_THE_TESTABLE_FILE* e.g. *features-netsocket-InternetSocket*
207206
* **unittest-includes** - Include paths for headers needed to build the tests in addition to the base include paths listed in [CMakeLists.txt](CMakeLists.txt). Optional.
208207
* **unittest-sources** - Mbed OS source files and stubs included for the build.
209208
* **unittest-test-sources** - Unit test source files.
@@ -225,8 +224,6 @@ For example to create a unit test for `rtos/Semaphore.cpp`:
225224
1. Create a directory for unit test files in `UNITTESTS/rtos/Semaphore`.
226225
2. Create a test definition file `UNITTESTS/rtos/Semaphore/unittest.cmake` with the following content:
227226
```
228-
set(TEST_SUITE_NAME "rtos-Semaphore")
229-
230227
set(unittest-sources
231228
stubs/mbed_assert.c
232229
../rtos/Semaphore.cpp

UNITTESTS/features/cellular/framework/AT/at_cellularbase/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "cellular-framework-AT-AT_CellularBase")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/AT/AT_CellularBase

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_device_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_information_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_network_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_power_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_sim_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_sms_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_cellular_stack_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "at_handler_unit")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/cellular/framework/common/util/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "cellular-framework-common-util")
8-
96
# Add test specific include paths
107
set(unittest-includes ${unittest-includes}
118
features/cellular/framework/common/util

UNITTESTS/features/netsocket/InternetSocket/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "features-netsocket-InternetSocket")
8-
96
set(unittest-sources
107
../features/netsocket/SocketAddress.cpp
118
../features/netsocket/NetworkStack.cpp

UNITTESTS/features/netsocket/NetworkInterface/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "features-netsocket-NetworkInterface")
8-
96
# Source files
107
set(unittest-sources
118
../features/netsocket/NetworkInterface.cpp

UNITTESTS/features/netsocket/TCPSocket/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "features-netsocket-TCPSocket")
8-
96
set(unittest-sources
107
../features/netsocket/SocketAddress.cpp
118
../features/netsocket/InternetSocket.cpp

UNITTESTS/features/netsocket/UDPSocket/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "features-netsocket-UDPSocket")
8-
96
set(unittest-sources
107
../features/netsocket/SocketAddress.cpp
118
../features/netsocket/NetworkStack.cpp

UNITTESTS/platform/CircularBuffer/unittest.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "platform-CircularBuffer")
8-
96
set(unittest-sources
107
)
118

UNITTESTS/template/unittest.cmake.template

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# UNIT TESTS
44
####################
55

6-
# Unit test suite name
7-
set(TEST_SUITE_NAME "suitename")
8-
96
set(unittest-includes ${unittest-includes}
107
headerfile
118
)

UNITTESTS/unit_test/new.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class UnitTestGeneratorTool(object):
2828
Generator tool to create new unit tests from template
2929
"""
3030

31-
def _replace_content(self, template_content, dirname, classname, suite_name, extension):
31+
def _replace_content(self, template_content, dirname, classname, extension):
3232
if extension == "h":
3333
content = re.sub(r"cppfile",
3434
"",
@@ -41,7 +41,6 @@ def _replace_content(self, template_content, dirname, classname, suite_name, ext
4141
content = re.sub(r"headerfile", "../dirname/template.h", content)
4242
content = re.sub(r"dirname", dirname, content)
4343
content = re.sub(r"template", classname, content)
44-
content = re.sub(r"suitename", suite_name, content)
4544

4645
return content
4746

@@ -111,7 +110,6 @@ def create(self, mbed_os_root=None, filepath=None):
111110
content = self._replace_content(template_content,
112111
dir_name,
113112
class_name,
114-
suite_name,
115113
file_extension)
116114

117115
output_file.writelines(content)
@@ -130,7 +128,6 @@ def create(self, mbed_os_root=None, filepath=None):
130128
content = self._replace_content(template_content,
131129
dir_name,
132130
class_name,
133-
suite_name,
134131
file_extension)
135132

136133
output_file.writelines(content)

0 commit comments

Comments
 (0)