Question

I've got a UnitTest++ test class, which allows me to test that a class is parsing some strings correctly. Before running the test, I create a fixture that contain several strings to be tested by various functions in a loop. It seems to work fine, but the problem is that, in case of error, UnitTest++ will always give me the same error line, so I won't know which string exactly is causing the issue.

For example, it will output:

"[UnitTest++] ..\trunk\tests\Test_ChineseUtil.cpp(46): error: Failure in ParsePinyinT: ChineseUtil::parsePinyinT(pinyinT) == pinyinN" 

But that doesn't tell me which string is not being parsed correctly.

So what I would like is to set some custom error message when a test fails (in that particular case, I would give it the first item in my array). Basically, I need something like:

CHECK(theTest, "my error message")

Is there such function in UnitTest++? Or maybe there is a better way to do what I'm trying to do?

For information, here is the code of my class:

#include <third_party/unittest++/UnitTest++.h>

#include <Application.h>
#include <ChineseUtil.h>

using namespace hanzi;

namespace chineseUtilTests {

class PinyinFixture {

public:

    PinyinFixture() {
        ChineseUtil::initialize();

        testData << "third tone" << QString::fromUtf8("wo3") << QString::fromUtf8("wǒ");
        testData << "no tone" << QString::fromUtf8("wo") << QString::fromUtf8("wo");
        testData << "second tone" << QString::fromUtf8("guo2") << QString::fromUtf8("guó");
        testData << "first tone" << QString::fromUtf8("jia1") << QString::fromUtf8("jiā");
        testData << "fifth tone" << QString::fromUtf8("jia5") << QString::fromUtf8("jia");
        testData << "two dots" << QString::fromUtf8("nu:") << QString::fromUtf8("nü");
        testData << "two dots and tone" << QString::fromUtf8("nu:3") << QString::fromUtf8("nǚ");
    }

    ~PinyinFixture() {

    }

    QStringList testData;

};

TEST_FIXTURE(PinyinFixture, ParsePinyinN) {
    for (int i = 0; i < testData.size(); i++) {
        QString pinyinN = testData[i][1];
        QString pinyinT = testData[i][2];
        CHECK(ChineseUtil::parsePinyinN(pinyinN) == pinyinT); 
    }
}

TEST_FIXTURE(PinyinFixture, ParsePinyinT) {
    for (int i = 0; i < testData.size(); i++) {
        QString pinyinN = testData[i][1];
        QString pinyinT = testData[i][2];
        CHECK(ChineseUtil::parsePinyinT(pinyinT) == pinyinN); 
    }
}

} // chineseUtilTests
Was it helpful?

Solution

If your class has an equality operator, you should be able to, instead of typing

CHECK(something == something_else);

use

CHECK_EQUAL( something, something_else);

The fact that your classes allow a == makes me thing you can do this. If the test fails, you should get something along the lines of "Expected something but got something else".

If you need more information, there are a couple other things you can do.

One is to add your own custom output in the test. If you need to know the value of i specifically, you can add a printout of i before each CHECK. However, this output may be too lengthy for large loops, so you can add your check a second time.

CHECK(ChineseUtil::parsePinyinN(pinyinN) == pinyinT);
if ( ChineseUtil::parsePinyinN(pinyinN) != pinyinT )
{
    cout << "Your own custom message " << i << endl;
}

Another possibility is to modify the source code of UnitTest++ itself, then recompile the library. While I don't know the exact steps to do what you want to do, I've modified UnitTest++/src/Checks.h to improve the output of CHECK_ARRAY2D_CLOSE so it was more readable.

OTHER TIPS

I have not compiled or tested the following, but the gist of it is to copy the UnitTest++ CHECK macro and expand it to take a message parameter that is std::string and combine that with the string representing the stringified version of value.

#define CHECK_MESSAGE(value, message) \
    do \
    { \
        try { \
            if (!UnitTest::Check(value)) \
                UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), (std::string(#value)+message).c_str()); \
        } \
        catch (...) { \
            UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
                    (std::string("Unhandled exception in CHECK(" #value)+message+std::string(", ")+message+std::string(")")).c_str()); \
        } \
    } while (0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top