Tuesday Coding Tip 10— CTest

posted Originally published at towardsdev.com 1 min read

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


CMake can do many things and instrumenting your test suite is one of them. Use add_test to tell cmake it should run a particular command with given parameters. If it returns zero, it succeeded. You can use it to simply run a testrunner binary or to make a matrix of input parameters that are expected to not crash the program.

cmake_minimum_required ( VERSION 3.26 )

project ( testrunner )
add_executable ( ${PROJECT_NAME} Main.cpp )

enable_testing()
add_test(
   NAME "Example with no params"
   COMMAND ${PROJECT_NAME}
)

add_test(
   NAME "Example with help param"
   COMMAND ${PROJECT_NAME} -h
)

In order to add_test do anything, one has to also call enable_testing(). Running the tests is just a matter of running ctest from command line. Just note that ctest requires you to specify a build configuration if you have more than one.

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 08 — Explicit template specialization

Jakub Neruda - Apr 21

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31

Tuesday Coding Tip 12 — Stack trace

Jakub Neruda - May 19
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!