Test Driven Development with C++
Asked Answered
R

3

23

Looking to start doing TDD in C++. I've seen CPPUnit, but I was wondering if there are other options that people prefer?

Thanks for your suggestions!

Rueful answered 15/3, 2011 at 4:48 Comment(2)
Are you just looking for C++ unit test frameworks? https://mcmap.net/q/67991/-c-unit-testing-framework-closedSimmons
Looks great. Wanna post as an answer, and I'll accept?Rueful
H
30

I can recommend Google Mock, which has become part of Google Test bundled. We switched from UnitTest++ to Google Test/Google Mock a couple of years ago and have never looked back.

Google Mock can be used even if you don't want to use the mocking facilities. Its matchers are very useful.

Hurdygurdy answered 15/3, 2011 at 18:0 Comment(2)
I'm not seeing any way to add time constraints which is a pretty big deal in my testing. Am I just missing it?Hessite
I guess the easiest way would be to measure elapsed time for the code you want to test and then use EXPECT_LT to verify your timing requirement.Hurdygurdy
S
10

I switched from CppUnit to boost::test some years ago and I'm much happier with it.

  • Documentation for CppUnit is nonexistent. Good luck trying to find out what command line options it supports without reading the code. Apparently it makes more sense to people already familiar with JUnit though. boost::test has excellent documentation.
  • boost::test's auto test registration facility makes adding unit test cases insanely easy. With CppUnit you have to write quite a lot of boilerplate for each test case (a line in the header and a line it the .cpp to register it, on top of the test method itself).
  • boost::test lets you select test subsets by regexp from the commandline. We had to hack CppUnit sources to get it to do that when we originally picked it up.
  • The one thing I do miss from CppUnit is its "Protectors". You can define your own and have them wrap each test and check whatever (e.g we had a problem with some code messing with the x87 floating point rounding mode; checking the state was unchanged in a Protector quickly caught all offenders). boost::test has some similar thing called a test_observer but last time I tried you couldn't actually fail a test from one.
Smite answered 15/3, 2011 at 13:54 Comment(1)
I like gtest better than CppUnit for almost all of the same reasons here, gtest also wraps each test in SEH so access violations don't stop all tests.Cousins
S
6

If you are just looking for C++ unit test frameworks, see this question and its answers: C++ unit testing framework

Simmons answered 15/3, 2011 at 13:41 Comment(3)
Note that the comment in that thread that boost::test cannot run tests by name is no longer accurate.Smite
@timday: Thanks for pointing that out. @hahuang65: The accepted answer there isn't necessarily the best. I'd recommend reviewing all the answers.Simmons
Will do. Thanks for all the answers guys.Rueful

© 2022 - 2024 — McMap. All rights reserved.