Pretty print Qt classes in Googletest

  • I am attempting to dump Qt classes in the Googletest framework.

    The basics of how to pretty print custom classes in Googletest is "here":

    I've tried an example with QSize, but have been unable to pretty print it so far.

    My code attempt:

    void PrintTo (const QSize& size, ::std::ostream* stream)
    const QString dump = QString("[Size w:") % QString::number(size.width()) %
    QString(" h:") % QString::number(size.height()) %
    *stream << dump.toStdString();


    However, my output is this:

    @....\tpeb\test\tpeb_test_graphiccomponents.cpp(475): error: Value of: preferredSize.toSize()
    Actual: 8-byte object <80-00 00-00 80-00 00-00>
    Expected: QPixmap(pixmapName).size()
    Which is: 8-byte object <18-00 00-00 18-00 00-00>@

    Any ideas?

  • Hi everyone,

    I have a similar problem. I try to print a QString, for which I implemented a PrintTo method

    @void PrintTo(const QString& str, ::std::ostream* os)
    *os << "QString(" << str.toStdString() << ")";

    But I also still get the default output of google test

    [----------] 1 test from One
    [ RUN ] One.Two
    ../UnitTests/tests/printtotest.cpp:21: Failure
    Value of: second
    Actual: { 2-byte object <57-00>, 2-byte object <6F-00>, 2-byte object <72-00>, 2-byte object <6C-00>, 2-byte object <64-00> }
    Expected: first
    Which is: { 2-byte object <68-00>, 2-byte object <65-00>, 2-byte object <6C-00>, 2-byte object <6C-00>, 2-byte object <6F-00> }
    [ FAILED ] One.Two (0 ms)

    Does anyone have an idea why we cannot get this to work although in the "googletest documentation": this is described as the way to go?

  • Since that posting, I made it work, although I don't remember exactly how I did that, unfortunately.

    One hint I found in my code is that the PrintTo must not be declared/defined within a namespace. It's possible that this was my original problem.

  • Hi Asperamanca,

    thank you for your answer. In my code, the PrintTo function is not defined inside a namespace. Could you maybe post a part of your code that is already working? That would be a great help for me. Thanks!

  • I would have posted them right away had I seen something interesting. But here you go:

    Example for pretty printing of QString
    @void PrintTo(const QString& dumpObject, ::std::ostream* os);@
    @void PrintTo(const QString& dumpObject, std::ostream* os)
    *os << dumpObject.toStdString();

    Nothing else really. No defines, no namespaces before that code. Just including the header file in the cpp file.

  • Hm, that is the same way I did it, but it doesn't seem to work for me. Maybe it's a compiler specific issue? I use clang on MacOS and Qt 5.3.0

  • Windows and MSVC 2010. In my case, I do have the PrintTo in a separate library. Although I feel having them in the same project should actually make it easier, not harder.

  • Funny thing is, it works with a QDateTime object, like this:

    @void PrintTo(const QDateTime& dumpObject, std::ostream* os)
    *os << dumpObject.toString().toStdString();

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.