14

I am using the GoogleTest (GTest) framework in conjunction with a Qt5 application.

Whenever a test fails using a QString argument, the framework tries to print all the involved values. However, it cannot automatically handle foreign types (Qt5's QString in this case).

QString test = "Test";
ASSERT_EQ(test, "Value");

enter image description here

How can I get GoogleTest to pretty print QStrings automatically (= without having to manually convert them each time)?

1 Answer 1

22

The GoogleTest Guide explains how you can in general teach the framework to handle custom types.

In the end, the following code snippet is all that needs to be added for GoogleTest being able to work with QStrings:

QT_BEGIN_NAMESPACE
inline void PrintTo(const QString &qString, ::std::ostream *os)
{
    *os << qUtf8Printable(qString);
}
QT_END_NAMESPACE

This code MUST NOT be in the namespace of your test fixtures, but must be in the Qt namespace (or in general in the namespace where the type that should be pretty-printed is defined in). This code MUST also be viewable from all translation units where you call a GoogleTest assertion on that particular type, otherwise it will not get used (see comments).

As a result GoogleTest now pretty prints QStrings: enter image description here

You can of course also add some quotation marks to make it clearer that it comes from a QString:

*os << "\"" << qUtf8Printable(qString) << "\"";

Source: Webinar ICS Qt Test-Driven Development Using Google Test and Google Mock by Justin Noel, Senior Consulting Engineer

7
  • 1
    Some other tip: use qUtf8Printable if you also want print Unicode characters in your string (qPrintable converts to "local 8 bit", which may not be Unicode clean, especially on Windows); and go through QDebug (possibly acting on a QBuffer, hosting a QByteArray) if you want to print QStrings and escape the non-printable characters.
    – peppe
    Mar 5, 2017 at 9:03
  • 2
    I can NOT get this to work. The funny thing is that if I define two PrintTo functions (one taking a QString& and one taking a regular QString) I get a compiler error error C2668: 'PrintTo': ambiguous call to overloaded function could be 'void PrintTo(const QString,std::ostream *)' or 'void PrintTo(const QString &,std::ostream *)' however if I have just the one never seems to call it... what's the magic sauce?
    – gremwell
    Mar 8, 2017 at 4:00
  • @gremwell Did you ensure that PrintTo is not in your applications namespace? Apart from I don't know what the sauce might be. I just put those lines before my first fixture is declared in the same CPP file and it just works (QT 5.7 and latest GoogleTest). Mar 8, 2017 at 8:08
  • 3
    @Gremwell, I was struggling with this also but the answer to this question solved it for me stackoverflow.com/questions/24673515/…
    – Eelke
    Sep 9, 2018 at 15:33
  • @Eelke I had the same problem with the translation units. I have edited the answer accordingly.
    – fkorsa
    Nov 28, 2019 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.