How do you implement unit tests in cpp?

This is a discussion on How do you implement unit tests in cpp? within the C++ Programming forums, part of the General Programming Boards category; Do you use some special framework (I saw boost is offering something)? Or do you just add a main() to ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    How do you implement unit tests in cpp?

    Do you use some special framework (I saw boost is offering something)?
    Or do you just add a main() to every class hidden by an #ifdef UTEST or something like that?
    Or something completely different I still can't think of?
    A little example would be great.

    Thank you in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    33,292
    The possibilities are endless....

    > Or do you just add a main() to every class hidden by an #ifdef UTEST or something like that?
    I'm using this approach at the moment, except for a file called foo.cpp, the function at the end is called foo_test()

    These are all collected together in a single main() which can run them all, or choose which one(s) to run.


    Or you could create a class.test() member function in each class, then just do
    myclass var.test();
    to invoke the test for that class.

    Like I said, there are many possibilities.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are various frameworks, too. Boost.Test, which you saw, is one. I saw another (but forgot the name) announced on comp.lang.c++.moderated today (use Google Groups to search for unit test).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    I saw another (but forgot the name) announced on comp.lang.c++.moderated today
    FRUCTOSE? One that I have been looking at is UnitTest++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2006, 07:20 PM
  2. newbie here.. pls help me in this program!
    By rothj0hn in forum C Programming
    Replies: 2
    Last Post: 02-01-2006, 09:40 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. 2-d object avoidance. Help please! (basic stuff, I think)
    By charbach007 in forum Game Programming
    Replies: 4
    Last Post: 06-15-2004, 03:49 PM
  5. Unit Actions
    By DavidP in forum Game Programming
    Replies: 20
    Last Post: 05-28-2004, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21