QCOMPARE with Custom Qt Types
When you write unit tests, you will have to compare the actual value and the expected value. A simplified example with QString
s would look like this.
void MyTest::testQCompare() {
auto actualStr = QString{"abba"};
auto expectedStr = QString{"juhu"};
QCOMPARE(actualStr, expectedStr);
}
When you run this unit test, the QTest
framework will print the actual value "abba"
and the expected value "juhu"
. This is often enough to know what went wrong.
FAIL! : MyTest::testQCompare() Compared values are not the same
Actual (actualStr) : "abba"
Expected (expectedStr): "juhu"
If two values of a custom type differ, the QTest
framework will only print the first line: Compared values are not the same. This is not very helpful. How can you make QCOMPARE
print the actual and expected for custom types as well?