boost.test vs. CppUnit
Asked Answered
D

2

14

I've been using CppUnit for quite a while now (and am happy with it). As we are using more and more parts of the boost library I had a short look on boost.test and I'm wondering now if I should switch to boost.test in a new project or not.

Can anyone here tell me about the differences between the two frameworks and the benefits (if there are any) of using boost.test?

Divergent answered 23/6, 2010 at 9:9 Comment(3)
You'll probably find the answers to #243426 useful.Antipathy
@Alex: thanks for your post, there are some really helpful links in itDivergent
Good link, AAlex, +1. @chrmue, to clarify - should the comparision that are you looking for assume a Boost user or not? Are you asking "if I use Boost to develop, then is there an advantage to using Boost text?" or are you just looking for a straightforward comparision?Colleencollege
N
16

Do yourself a favor and go straight to Google Test, which makes CppUnit and boost::unit_test look clunky and repetitive.

For example, say you have a simple fixture:

class MyFixture : public ::testing::Test
{
  protected:
  int foo;

  virtual void SetUp() { foo = 0; }
};

To add a test to your fixture, write it!

TEST_F(MyFixture, FooStartsAtZero) {
  EXPECT_EQ(0, foo);
}

That's all you need. Notice the lack of explicit testsuite declarations or a separate agenda that repeats all your tests' names.

Compile it as in

$ g++ -o utest utest.cpp -lgtest -lgtest_main

and run your test to get

Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyFixture
[ RUN      ] MyFixture.FooStartsAtZero
[       OK ] MyFixture.FooStartsAtZero (0 ms)
[----------] 1 test from MyFixture (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

(Run it yourself to see the nice green text for passing tests!)

This is only the beginning. Take a look at the Google Test primer and the advanced guide to see what else is possible.

Noctule answered 27/6, 2010 at 0:1 Comment(3)
+1 I was asking myself the same question, and this answer has prompted me to look at Google Test. Thanks.Colleencollege
Thanks for posting this answer! It doensn't really answer my question, but it pointed me to a unit test framework that I didn't know yet and that looks really promising. What I like most at the first glance is the way how test cases are expressed: the shortest possible form!Divergent
@Divergent Thanks for considering it! You'll be thrilled with Google Test's power and expressiveness.Noctule
F
43

How is this less clunky than Boost.Test alternative:

class MyFixture { MyFixture() { /* setup here */} };

BOOST_AUTO_TEST_CASE( my_test, MyFixture )
{
    BOOST_CHECK_EQUAL(0, foo);
}

Macros indeed a bit longer, but this is safer and is recommended practice in C++.

I am yet to see a single technical reason to prefer Google Test (and I know quite a few to prefer Boost.Test). The rest is just your preference.

Faultfinder answered 17/3, 2011 at 0:36 Comment(3)
I use Boost.Test a lot. Can you elaborate on "I know quite a few to prefer Boost.Test"?Hydrated
Also curious as to why you prefer Boost.Test. What are the advantages over googletest?Fullgrown
What are those advantages?Dearborn
N
16

Do yourself a favor and go straight to Google Test, which makes CppUnit and boost::unit_test look clunky and repetitive.

For example, say you have a simple fixture:

class MyFixture : public ::testing::Test
{
  protected:
  int foo;

  virtual void SetUp() { foo = 0; }
};

To add a test to your fixture, write it!

TEST_F(MyFixture, FooStartsAtZero) {
  EXPECT_EQ(0, foo);
}

That's all you need. Notice the lack of explicit testsuite declarations or a separate agenda that repeats all your tests' names.

Compile it as in

$ g++ -o utest utest.cpp -lgtest -lgtest_main

and run your test to get

Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyFixture
[ RUN      ] MyFixture.FooStartsAtZero
[       OK ] MyFixture.FooStartsAtZero (0 ms)
[----------] 1 test from MyFixture (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

(Run it yourself to see the nice green text for passing tests!)

This is only the beginning. Take a look at the Google Test primer and the advanced guide to see what else is possible.

Noctule answered 27/6, 2010 at 0:1 Comment(3)
+1 I was asking myself the same question, and this answer has prompted me to look at Google Test. Thanks.Colleencollege
Thanks for posting this answer! It doensn't really answer my question, but it pointed me to a unit test framework that I didn't know yet and that looks really promising. What I like most at the first glance is the way how test cases are expressed: the shortest possible form!Divergent
@Divergent Thanks for considering it! You'll be thrilled with Google Test's power and expressiveness.Noctule

© 2022 - 2024 — McMap. All rights reserved.