C++ Unit Testing Libraries [closed]
Asked Answered
P

8

21

I've come across cppunit but it didn't look super-easy to use (maybe I didn't look hard, maybe because C++ doesn't work like Java/C#). Are there widely used, simple alternatives?

In fact, is cppunit the standard unit testing framework for C++?

Pitta answered 10/1, 2010 at 21:28 Comment(4)
Do you want support for mock objects?Devora
Related: #91884 #14199 #3650 https://mcmap.net/q/67950/-unit-testing-in-c-closed and probably othersNylanylghau
Duplicate of #243426Unasked
And yet, whenever duplicates come up we get new answers, so it's worthwhile unless someone merges answers to this thread into older duplicates.Pitta
G
23

There is no standard unit testing library for C++. There are many choices to choose from; cppunit being one of them.

At my company we use Google Test along with its partner Google Mock for unit testing and object mocking. I find them both combined easier to use and much more powerful than cppunit.

Galliett answered 10/1, 2010 at 21:31 Comment(3)
+1 Cool, I didn't know these existed.Lactone
Just curious, do the google-thangs "play well" with Eclipse and Hudson?Recreation
It is able to write output to xml files following the same schema as JUnit with the flag --gtest_output="xml:_path_to_output_file_". It works great with hudson for us on Windows, Mac, and Ubuntu. I can't speak for eclipse, though as we don't use that.Galliett
W
25

I've just pushed my own framework, CATCH, out there. It's still under development but I believe it already surpasses most other frameworks. Different people have different criteria but I've tried to cover most ground without too many trade-offs. Take a look at my linked blog entry for a taster. My top five features are:

  • Header only
  • Auto registration of function and method based tests
  • Decomposes standard C++ expressions into LHS and RHS (so you don't need a whole family of assert macros).
  • Support for nested sections within a function based fixture
  • Name tests using natural language - function/ method names are generated

It also has Objective-C bindings.

As an example, here's how you'd write the equivalent of the gtest example that @dmeister cited:

TEST_CASE( "Factorial/Handles zero input", "Tests factorial of 0.") 
{
  REQUIRE( Factorial(0) == 1 );
}

TEST_CASE( "Factorial/Handles positive input", "Tests factorial of positive numbers.") 
{
  REQUIRE( Factorial(1) == 1 );
  REQUIRE( Factorial(2) == 2 );
  REQUIRE( Factorial(3) == 6 );
  REQUIRE( Factorial(8) == 40320 );
}

If a test failed you'd still get the LHS and RHS values logged independentaly.

Week answered 28/12, 2010 at 3:18 Comment(1)
doctest is my reimplementation of Catch with a huge focus on compilation speed - checkout the FAQ to see whats different between the twoSennit
G
23

There is no standard unit testing library for C++. There are many choices to choose from; cppunit being one of them.

At my company we use Google Test along with its partner Google Mock for unit testing and object mocking. I find them both combined easier to use and much more powerful than cppunit.

Galliett answered 10/1, 2010 at 21:31 Comment(3)
+1 Cool, I didn't know these existed.Lactone
Just curious, do the google-thangs "play well" with Eclipse and Hudson?Recreation
It is able to write output to xml files following the same schema as JUnit with the flag --gtest_output="xml:_path_to_output_file_". It works great with hudson for us on Windows, Mac, and Ubuntu. I can't speak for eclipse, though as we don't use that.Galliett
Z
6

Google Test Framework is an alternative.

Here is a simple example from the documentation:

// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
  EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}

It also plays nicely with gmock, Google's mock framework for C++.

Zone answered 10/1, 2010 at 21:34 Comment(1)
I've given an example of how you'd write the equivalent of these tests in my own framework, CATCH, in my answer.Week
B
2

CppUnit probably is the first unit test framework for C++. It's a direct port of Junit, the famous Java framework. This makes the transition from Junit easier, but at the cost of a somewhat heavy framework, that does not take advantage of C++ capability such as RAII. That's the reason why lightweight versions such as CppUnitLite, NanoCppUnit have been created. CppUnit2 was supposed to improve this, among other improvements.

Tut used to be very light, just one header, but the latest versions introduced a library.

As far as a "standard" framework is concerned, there is none, and C++1X does not define one.

Bushelman answered 11/1, 2010 at 6:11 Comment(0)
B
2

I created a testing suite called saru ( http://github.com/mikeando/saru ) for my own code dev. Its a BSD licensed code. I developed it as I didn't like several of the features of other testing suites. Its not widely used, but I've used it on several commercial projects spread across two companies.

  1. I don't like all my tests getting compiled into one binary. My reasons for this are if the compile fails all tests fail, if one test does undefined behaviour the program output is undefined.
  2. I want control over what tests run. I want to be able to group tests and run subsets.
  3. I want compilation failure of a test to be reported as a test failure, and not halt all my tests running.
  4. I want to be able to run tests from multiple different languages
  5. I want a system flexible enough that I can run specific tests under valgrind (not yet in saru :( )

So saru addresses most of these features. Its focus is on being able to run a suite of tests written in different languages. With minimal test sizes. Here's the smallest (failing) C++ test

//SARU : dummy dummy
int main() { return (1==2)?0:1; }

All saru really cares about is the return value of the binary that it compiles. It then parses the output to determine what tests failed and so on. It has headers to make working with C++ a little nicer than the above trivial example :

//SARU : dummy dummy
#include "MyStruct.h"
#include "saru_cxx.h"

class Fixture
{
  MyStruct s_;
  Fixture() : s_() {}
  void test_A_is_B()
  {
     SARU_ASSERT_EQUAL( s_.A(), s_.B() );
  }

  void test_C_is_7()
  {
     SARU_ASSERT_EQUAL( 7, s_.C() );
  }
};

int main()
{
   saru::TestLogger logger;
   SARU_TEST( Fixture:: test_A_is_B, logger );
   SARU_TEST( Fixture:: test_C_is_7, logger );
   logger.printSummary();
   return logger.allOK()?0:1;
}

Or if you don't like the way its C++ headers work it should be able to integrate with other unittesting libraries with minimal difficulty.

But it will also run tests that are written in PHP & python. So you can set up full functional tests with saru. Or you can run something like lint over your code as part of the test suite.

Beslobber answered 11/1, 2010 at 8:35 Comment(0)
E
1

Here is a single header file only include solution for C++ unit testing: https://gitlab.com/cppocl/unit_test_framework

Simple example of using it here, but it also has fixtures (setup and teardown), fail test on memory leak, fail test on performance (I've not seen this feature anywhere else).

#include "test/Test.hpp"

TEST(MyTest)
{
    int a = 1;
    int b = 2;
    CHECK_EQUAL(a + b, 3);
}
Endocrinology answered 5/6, 2018 at 21:36 Comment(0)
T
0

Here's a list of unit testing libraries.

http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B

However, as far as I know, cppunit is the most popular.

Ta answered 10/1, 2010 at 21:31 Comment(0)
K
0

Here's a minimal C++ unit testing library: https://github.com/vahidk/minimal-cpp-test

It has very similar syntax to Google test library but it's a header only library and therefore easier to port across platforms.

Here's a minimal unit test:

#define DEFINE_TEST_MAIN    
#include "test.h"
TEST(GroupName, TestName) {
  EXPECT_EQ(1 + 2, 3);
}

And a minimal fixture:

class ClassName : public cyrus:Test {
public:
  void Setup() override { 
    x = 5;
  }
  int x;
}

Test_F(ClassName, Test1) {
  EXPECT_EQ(x, 5);
}

Hope this helps.

Krever answered 2/3, 2015 at 22:16 Comment(1)
The link gives 404Birdsong

© 2022 - 2024 — McMap. All rights reserved.