19

Been hitting my head on the wall before as I don't make any test classes while using c/c++ (but instead have a lot of print methods).

What is the most used method to perform testing in the c/c++ code? Java's JUnit has only left me with good memories while debugging things.

I know that using asserts in code with a defined debug header should work, but aren't there any better ways?

And since I have the intention to make test classes in the future, any useful tips are welcome.

Milan
  • 15,389
  • 20
  • 57
  • 65

15 Answers15

15

We use Google Test and it's companion Google Mock. Works wonderfully and supports JUnit style XML output for easy integration with CruiseControl, etc. It's also fully cross platform, and from my research a few months ago, GMock was the ONLY fully cross platform object mocking framework for C++.

Grant Limberg
  • 20,913
  • 11
  • 63
  • 84
13

We use boost.Test. There is also cppunit.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
chrish
  • 2,352
  • 1
  • 17
  • 32
9

You can check these out:

http://gamesfromwithin.com/?p=29

http://www.opensourcetesting.org/unit_c.php

http://msdn.microsoft.com/en-us/magazine/cc136757.aspx

bbqchickenrobot
  • 3,592
  • 3
  • 45
  • 67
  • First link to gamesfromwithin has all I needed to know about different frameworks (at least for now). Thank you. – Milan Jul 01 '09 at 14:51
  • 1
    Keep in mind that the gamesfromwithin.com article, although excellent, was written in 2004, and things have changed since then. – Josh Kelley Apr 08 '11 at 12:24
  • 1
    For UnitTest++ go to github.com/unittest-cpp/unittest-cpp. Everything else is out of date. – Markus Sep 17 '13 at 11:34
3

UnitTest++ is worth a look. It lacks features compared to some of the other frameworks mentioned here, but the simplicity is nice -- especially if you're just getting started.

I believe it was developed by Noel Llopis after writing the article mentioned in http://gamesfromwithin.com/?p=29. It is pretty easy to port to a multitude of different compilers/platforms -- useful if need to target something other than a PC, be it a game console, embedded device, or otherwise.

The mailinglist has been quiet for a while now, but every so often there are questions, patches, and/or a new release, and people who ask questions are usually answered quickly.

leander
  • 8,527
  • 1
  • 30
  • 43
  • 1
    Seconded. I've tried a handful of unit test frameworks for C++, and UnitTest++ is easily the best in my opinion. You are correct that it was developed by Noel Llopis. See http://gamesfromwithin.com/?p=51. – Geerad Jul 01 '09 at 05:20
2

Wikipedia has a long list of unit testing frameworks for C++ and another list for C.

David Johnstone
  • 24,300
  • 14
  • 68
  • 71
2

I'm working with Qt under Windows and Linux, wich provides an own integrated (but compared to other separate solutions a little bit limited) testing framework.

It's very easy to use and fast to learn - see QTestLib

ciao, Chris

3DH
  • 1,461
  • 11
  • 11
1

Another problem people grossly over-engineer.

The below is a top-of-my-head, rough, non-working implementation of a nominal unit test infrastructure. It makes some sense to construct Test with a std::string namespace/class name and use a consistent naming scheme to print out which test is failing. I just use a unit test template and have some scripts that find all the executables starting with u (one per class), runs them in turn and collects the return values.

struct Test
{
   std::vector<(std::string)fptr*(void)> tests;
   int run() const
   {
      int stat(0);
      for (/* tests */)
      {
         std::string const res(test.run());
         if (!res.empty())
         {
             std::cerr << res;
             ++stat;
         }
      }
      return stat;
   }
};

std::string test1()
{
   std::ostringstream oss;
// do a bunch of stuff
// mark some of this code to be pulled into the class documentation 

   // conditional checks follow
   if (fail)
   {
      oss << "fail: some reason" << std::endl;
   }
// more checks
   return oss.str();
}

int main(void)
{
   Tests tests;
   tests.push_back(test1);
   tests.push_back(test2);
   return tests.run();
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Brian
  • 81
  • 1
  • 5
1

Shameless plug: If you target Windows and are using Visual Studio, check out cfix and cfix studio.

It is also compatible to WinUnit.

Johannes Passing
  • 2,715
  • 16
  • 13
1

newest version of Visual Studio has something:

http://msdn.microsoft.com/en-us/library/ms243147(VS.80).aspx

MadH
  • 1,498
  • 4
  • 21
  • 29
1

Why not use CppUnit? It was created as a port of JUnit.

nathan
  • 5,513
  • 4
  • 35
  • 47
0

if you are using windows, take a look at: Limitations of using C++/CLI with NUnit and write your tests in http://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B.

also: http://golios.blogspot.com/2008/12/using-nunit-with-c-part-2.html, http://golios.blogspot.com/2008/11/using-nunit-with-native-c.html

this will work just like junit except for the fact that nunit does not create a new instance of the test case class before running each test.

Community
  • 1
  • 1
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

if it's for Windows platform you can try WinUnit from MS from here. Simplified Unit Testing for Native C++ Application

0

For Symbian there is SymbianOSUnit: http://www.symbianosunit.co.uk/

Riussi
  • 663
  • 1
  • 4
  • 6
0

Try using Catch. http://catch-lib.net

It's simple to use and setup and powerful enough for most.

0

In 2019 :