Back to Basics: Testing in C++ - Phil Nash - CppCon 2023

Discover the importance of testing in C++ and how to write effective tests, including separating act and assertion, minimizing cyclomatic complexity, and leveraging test frameworks for fast, reliable, and consistent results.

Key takeaways
  • Testing in C++ is a rapidly expanding area
  • Writing a test is about separating the act that you’re testing from the assertion of the expected result
  • Test frameworks should be fast, reliable, and consistent
  • Code should have low cyclomatic complexity to make tests easier to write
  • Separate concerns and encapsulate to make code testable
  • Focus on unit testing, which should be fast, reliable, and consistent
  • Don’t tolerate slow unit tests; they’re a waste of time
  • Tests should provide clear and useful feedback
  • Consider the design principles: keep it simple, loose coupling, separation of concerns, self-containment, and testability
  • Tests should be self-documenting and concise
  • Avoid global variables and setup; it’s a smell
  • Don’t use unnecessary mutation; it makes tests harder to read
  • Don’t mix concerns; isolate and separate
  • Use the testing pyramid: unit tests, integration tests, and system tests
  • Tests should provide fast and useful feedback, so run them frequently
  • Avoid headaches by testing early and often
  • Use a separate test framework, like Google Test or CATCH, to improve testing efficiency