Tuesday Coding Tip 10— CTest

●5 ●34 ●98
calendar_today • schedule1 min read
— Originally published at towardsdev.com

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.

Want more #cpp & #tuesday-coding-tips deep dives?

Join 6,093+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 10 of 32 in Tuesday Coding Tips

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

Let's play with C++ reflection

Jakub Neruda - Nov 12

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30
chevron_left
2.1k Points • 137 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
43Posts
38Comments
25Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Commenters (This Week)

26 comments
3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!