Skip to content

Commit c444468

Browse files
committed
Fix #8, Implement coverage test for SAMPLE_App
Use the UT Assert framework and stubs provided by other modules to perform unit testing on the SAMPLE_APP. This gets 100% line coverage on the current implementation.
1 parent 1f84f20 commit c444468

8 files changed

Lines changed: 831 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@ include_directories(fsw/platform_inc)
88
# to call library-provided functions
99
include_directories(${sample_lib_MISSION_DIR}/fsw/public_inc)
1010

11-
aux_source_directory(fsw/src APP_SRC_FILES)
11+
# Note that it is generally preferred to list the source files
12+
# explicitly rather than globbing using aux_source_directory()
13+
# The latter was also picking up sample_table.c which is not
14+
# intended to be built/linked with the app itself.
15+
set(APP_SRC_FILES fsw/src/sample_app.c)
1216

1317
# Create the app module
1418
add_cfe_app(sample_app ${APP_SRC_FILES})
1519

1620
# Add table
1721
add_cfe_tables(sampleTable fsw/src/sample_table.c)
22+
23+
# If UT is enabled, then add the tests from the subdirectory
24+
# Note that this is an app, and therefore does not provide
25+
# stub functions, as other entities would not typically make
26+
# direct function calls into this application.
27+
if (ENABLE_UNIT_TESTS)
28+
add_subdirectory(unit-test)
29+
endif (ENABLE_UNIT_TESTS)
30+

fsw/src/sample_app.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
#include "sample_table.h"
3535

3636
/* The sample_lib module provides the SAMPLE_Function() prototype */
37-
#include <sample_lib.h>
38-
3937
#include <string.h>
38+
#include "sample_lib.h"
4039

4140
/*
4241
** global data

fsw/src/sample_table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
**
3-
** GSC-18128-1, "Core Flight Executive Version 6.6"
3+
** GSC-18128-1, "Core Flight Executive Version 6.7"
44
**
55
** Copyright (c) 2006-2019 United States Government as represented by
66
** the Administrator of the National Aeronautics and Space Administration.

fsw/src/sample_table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************
22
**
3-
** GSC-18128-1, "Core Flight Executive Version 6.6"
3+
** GSC-18128-1, "Core Flight Executive Version 6.7"
44
**
55
** Copyright (c) 2006-2019 United States Government as represented by
66
** the Administrator of the National Aeronautics and Space Administration.

unit-test/CMakeLists.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
##################################################################
2+
#
3+
# Coverage Unit Test build recipe
4+
#
5+
# This CMake file contains the recipe for building the sample unit tests.
6+
# It is invoked from the parent directory when unit tests are enabled.
7+
#
8+
##################################################################
9+
10+
#
11+
#
12+
# NOTE on the subdirectory structures here:
13+
#
14+
# - "inc" provides local header files shared between the coveragetest,
15+
# wrappers, and overrides source code units
16+
# - "coveragetest" contains source code for the actual unit test cases
17+
# The primary objective is to get line/path coverage on the FSW
18+
# code units.
19+
# - "wrappers" contains wrappers for the FSW code. The wrapper adds
20+
# any UT-specific scaffolding to facilitate the coverage test, and
21+
# includes the unmodified FSW source file.
22+
#
23+
24+
set(UT_NAME sample_app)
25+
26+
# Use the UT assert public API, and allow direct
27+
# inclusion of source files that are normally private
28+
include_directories(${osal_MISSION_DIR}/ut_assert/inc)
29+
include_directories(${PROJECT_SOURCE_DIR}/fsw/src)
30+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
31+
32+
# The APP_SRC_FILES variable should contain the list of source files for the FSW build
33+
# There should be a 1:1 relationship between FSW source units and coverage tests
34+
# Generate a dedicated "testrunner" executable that executes the tests for each FSW code unit
35+
# Although sample_app has only one source file, this is done in a loop such that
36+
# the general pattern should work for several files as well.
37+
foreach(SRCFILE ${APP_SRC_FILES})
38+
get_filename_component(UNITNAME "${SRCFILE}" NAME_WE)
39+
40+
set(TESTNAME "${UT_NAME}-${UNITNAME}")
41+
set(UNIT_SOURCE_FILE "${CFE_SAMPLE_APP_SOURCE_DIR}/fsw/src/${UNITNAME}.c")
42+
set(TESTCASE_SOURCE_FILE "coveragetest/coveragetest_${UNITNAME}.c")
43+
44+
# Compile the source unit under test as a OBJECT
45+
add_library(ut_${TESTNAME}_object OBJECT
46+
${UNIT_SOURCE_FILE}
47+
)
48+
49+
# Apply the UT_C_FLAGS to the units under test
50+
# This should enable coverage analysis on platforms that support this
51+
set_target_properties(ut_${TESTNAME}_object PROPERTIES
52+
COMPILE_FLAGS "${UT_C_FLAGS}")
53+
54+
# Compile a test runner application, which contains the
55+
# actual coverage test code (test cases) and the unit under test
56+
add_executable(${TESTNAME}-testrunner
57+
${TESTCASE_SOURCE_FILE}
58+
$<TARGET_OBJECTS:ut_${TESTNAME}_object>
59+
)
60+
61+
# This also needs to be linked with UT_C_FLAGS (for coverage)
62+
set_target_properties(${TESTNAME}-testrunner PROPERTIES
63+
LINK_FLAGS "${UT_C_FLAGS}")
64+
65+
# This is also linked with any other stub libraries needed,
66+
# as well as the UT assert framework
67+
target_link_libraries(${TESTNAME}-testrunner
68+
ut_sample_lib_stubs
69+
ut_cfe-core_stubs
70+
ut_assert
71+
)
72+
73+
# Add it to the set of tests to run as part of "make test"
74+
add_test(${TESTNAME} ${TESTNAME}-testrunner)
75+
76+
endforeach()
77+

0 commit comments

Comments
 (0)