Valgrind support added for unittests

Added an option to select Valgrind to be used for unittests from command line
pull/12064/head
Antti Kauppila 2019-12-10 12:53:58 +02:00
parent 8c673ba597
commit 25a6e9d201
5 changed files with 38 additions and 8 deletions

View File

@ -82,6 +82,10 @@ if (COVERAGE)
endif(COVERAGE)
if (VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind)
endif(VALGRIND)
####################
# UNIT TESTS
####################
@ -196,6 +200,7 @@ foreach(testfile ${unittest-file-list})
if (unittest-test-sources)
# Create the executable.
add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
target_include_directories(${TEST_SUITE_NAME} PRIVATE
${unittest-includes})
target_compile_options(${TEST_SUITE_NAME} PRIVATE

View File

@ -76,13 +76,15 @@ def _mbed_unittest_test(options, cwd, pwd):
tool.create_makefiles(path_to_src=src_path,
generator=options.cmake_generator,
coverage_output_type=options.coverage,
debug=options.debug_build)
debug=options.debug_build,
valgrind=options.valgrind)
# Build tests
tool.build_tests()
if options.run_only:
tool.run_tests(filter_regex=options.test_regex)
tool.run_tests(filter_regex=options.test_regex,
valgrind=options.valgrind)
# If code coverage generation:
if options.coverage:

View File

@ -120,6 +120,8 @@ TEST_F(SlicingBlockModuleTest, slice_in_middle)
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
bd.read(buf, BLOCK_SIZE * 3, BLOCK_SIZE);
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
delete[] program;
}
TEST_F(SlicingBlockModuleTest, slice_at_the_end)
@ -143,6 +145,8 @@ TEST_F(SlicingBlockModuleTest, slice_at_the_end)
//Verify that blocks before and after the slicing blocks are not touched
bd.read(buf, BLOCK_SIZE * 7, BLOCK_SIZE);
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
delete[] program;
}
TEST_F(SlicingBlockModuleTest, over_write)
@ -163,6 +167,8 @@ TEST_F(SlicingBlockModuleTest, over_write)
//Program a test value to address that is one pass the device size
EXPECT_EQ(slice.program(program, 2 * BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
delete[] buf;
delete[] program;
}

View File

@ -103,6 +103,11 @@ def get_options_parser():
help="Build directory. Default: UNITTESTS/build/",
dest="build")
parser.add_argument("--valgrind",
help="Use Valgrind when running executables",
action="store_true",
dest="valgrind")
parser.add_argument("--new",
action="append",
help="Source file from which to generate test files. E.g. rtos/Semaphore.cpp",

View File

@ -59,7 +59,8 @@ class UnitTestTool(object):
path_to_src=None,
generator=None,
coverage_output_type=None,
debug=False):
debug=False,
valgrind=False):
"""
Create Makefiles and prepare targets with CMake.
@ -94,6 +95,12 @@ class UnitTestTool(object):
if coverage_output_type:
args.append("-DCOVERAGE:STRING=%s" % coverage_output_type)
if valgrind:
args.append("-DVALGRIND=1")
args.append("-DMEMORYCHECK_COMMAND_OPTIONS=\"--track-origins=yes\" \"--leak-check=full\" \"--show-reachable=yes\" \"--error-exitcode=1\"")
else:
args.append("-DVALGRIND=0")
if path_to_src is not None:
args.append(path_to_src)
@ -118,7 +125,7 @@ class UnitTestTool(object):
"Building unit tests failed.",
"Unit tests built successfully.")
def run_tests(self, filter_regex=None):
def run_tests(self, filter_regex=None, valgrind=False):
"""
Run unit tests.
@ -127,11 +134,16 @@ class UnitTestTool(object):
"""
args = [self.make_program, "test"]
if filter_regex:
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
if valgrind:
if filter_regex:
args.append("ARGS=-R %s -V -D ExperimentalMemCheck" % filter_regex)
else:
args.append("ARGS=-V -D ExperimentalMemCheck")
else:
args.append("ARGS=-V -D ExperimentalTest")
if filter_regex:
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
else:
args.append("ARGS=-V -D ExperimentalTest")
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
args.append("VERBOSE=1")