views:

207

answers:

3

I'm using UnitTest++ for unit testing C++ code.

In my tests, there's a group of tests I repeat several times. What I'd like is for a utility function to perform these tests. In short, I'd like to take this:

TEST( foo ) {
    Foo one;
    Foo two;

    // init one & two
    // lots of CHECK_CLOSE(one.bar, two.bar, 1e-5); in repeating cycles
}

TEST( bar ) {
    Foo one;
    Foo two;

    // init one & two
    // lots of CHECK_CLOSE(one.bar, two.bar, 1e-5); in repeating cycles
}

And use this:

void blah( const Foo& one, const Foo& two ) {
    // lots of CHECK_CLOSE(one.bar, two.bar, 1e-5);
}

TEST( foo ) {
    Foo one;
    Foo two;

    // init one & two
    blah(one, two);
}

TEST( bar ) {
    Foo one;
    Foo two;

    // init one & two
    blah(one, two);
}

This doesn't work due to UnitTest++'s macro manipulations. What is the best way to approach this problem?

edit: A couple of thoughts which I can't check out right now.

  1. If I use a fixture, with the utility function in the struct, will I be able to call UnitTest++ macros from within this function?
  2. I can write a macro to perform the common tests. I don't really like this, but at least I'll stary dry...
A: 

I would recommend against doing this, because what happens is you lose a lot of information about which test had failed, the wrapper function now encloses the functionality of two or more tests, so when it fails you have to trace back to see what was the reason for the failing test.

1800 INFORMATION
Yes, but the utility function can fail for different reasons, based on the parameters passed.
Gilad Naor
I agree, you will find that the worth of the utility or wrapper function is lessened because you then have to trace back to figure out why it failed and which test it was actually running. If you hardcode your tests this doesn't happen
1800 INFORMATION
So you would recommend repeating the tests several times in each test method? Hmmph, this seems quite a limitation off UnitTest++
Gilad Naor
This is kind of a limitation in all unit test frameworks. You have to do a tradeoff here
1800 INFORMATION
A: 

Make your common check function to return bool. And then CHECK or ASSERT result of your common function in each TEST.

Mykola Golubyev
This is what I'm doing now, but this way I lose information - I only know that one of the checks in the utility function failed, but not which one.
Gilad Naor
+1  A: 

Write a macro to do it. Don't let the "macros are evil" mantra hide this fact from you: Macros are designed to generate repetitive code.

Steve M