Is it okay to overload functions/methods for QTest unittests in QT5 C++?

104 Views Asked by At

Is it safe to overload functions/methods in QTest files?

I need the function/method to give a different result during the tests. The test works when I run it but will it cause any issues like so? or is there a better way to do it?

For QT5 QTest, Assuming we have Class1.cpp with:

QString Class1::func1() {
return "cat";
}

Is it okay to overload it like this for testing only in testClass1.cpp like so?

void TestClass1::testFunc1() {
QVerify(Class1::func1() == "bat")
}

QString Class1::func1() {
return "bat";
}
1

There are 1 best solutions below

0
On

You can use googlemock to write a mock of your class, as stated in the Qt doc https://doc.qt.io/qt-5/qttest-best-practices-qdoc.html#writing-testable-code