Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Google Test and printing QVector3D
klox last edited by
I have had a lot of success using google test's unit testing framework with Qt4/5. I have it set up with cmake and I am using QtCreator. One issue I am having is writing custom print functions. The documentation for it is "here":https://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values.
I have tried every which way I can think of, but no matter what gtest will not use my custom print functions for QVector3D. I define these at the top of my unit_tests.cpp file:
::std::ostream& operator<<(::std::ostream& os, const QVector3D& vector)
return os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
void PrintTo(const QVector3D& vector, ::std::ostream* os)
*os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
and then have a test case such as:
std::cout << "Testing 1 " << QVector3D(1,1,1) << std::endl;
std::cout << "Testing 2 ";
PrintTo(QVector3D(2, 2, 2), &std::cout);
std::cout << std::endl;
Which tosses this:
[ RUN ] Test.WhyTheHellWontQVector3DPrint
Testing 1 (1, 1, 1)
Testing 2 (2, 2, 2)
Value of: QVector3D(2,2,2)
Actual: 12-byte object <00-00 00-40 00-00 00-40 00-00 00-40>
Which is: 12-byte object <00-00 80-3F 00-00 80-3F 00-00 80-3F>
So it looks like my functions are correct since they successfully print the strings, but gtest isn't finding them. It just prints the byte-string of the object. I step through the code and it eventually gets to an internal fall-back printer
template <typename T> void PrintTo(const T& value, ::std::ostream* os) as part of the namespace ::testing::internal.
I have tried wrapping the functions in testing::internal and a ton of other mixes of namespaces. It just won't print it. Anyone have any ideas?
Thanks for your time and help!
Do you use a namespace Qt ? If that's the case you could try to surround the function definitions with:
Hope it helps
klox last edited by
Thanks for the suggestion. I didn't specify any namespace when compiling Qt 5.0.1 (I think that is where that would be done?), but I also gave that a shot since I saw those macros in qvector3d.h. No luck =/