Tuesday Coding Tip 10— CTest

2 5 25
calendar_todayschedule1 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.

837 Points32 Badges2 5 25
Brno, Czech Republiclinkedin.com/in/jakub-neruda
19Posts
10Comments
9Followers
9Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.
Build your own developer journey
Track progress. Share learning. Stay consistent.
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

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 15 — Mathematical constants in C++

Jakub Neruda - Jun 9
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!