Yeet
Yeet

Reputation: 92

QT Test using namespaces

I am using QT Test in QT5 and noticed none of the examples in their documentation (https://doc.qt.io/qt-5/qttestlib-tutorial1-example.html#writing-a-test) used a custom (non-QT) namespace, and really not even a QT namespace was referenced.

I tried using a namespace considering that is better practice for c++, but then the tests weren't working.

Here is the first example the QT documentation provided: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/qtestlib/tutorial1/testqstring.cpp?h=5.13

//! [0]
#include <QtTest/QtTest>

class TestQString: public QObject
{
     Q_OBJECT
private slots:
    void toUpper();
};
//! [0]

//! [1]
void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}
//! [1]

//! [2]
QTEST_MAIN(TestQString)
#include "testqstring.moc"
//! [2]

Is using namespaces I create something that should be done when using QT Test or is unnecessary?

Upvotes: 2

Views: 460

Answers (1)

Moia
Moia

Reputation: 2354

Not sure if I've understood well, maybe are you looking for this?

testqstring.cpp

#include <QtTest>

namespace Test {

   class TestQString: public QObject
   {
        Q_OBJECT
   private slots:
       void toUpper()
       {   
          QString str = "Hello";
          QCOMPARE(str.toUpper(), QString("HELLO"));
       }
   };

} // end namespace Test

QTEST_APPLESS_MAIN(Test::TestQString)

#include "testqstring.moc"

Note the macro QTEST_APPLESS_MAIN expands in

#define QTEST_APPLESS_MAIN(TestObject) \
int main(int argc, char *argv[]) \
{ \
    TestObject tc; \
    QTEST_SET_MAIN_SOURCE_PATH \
    return QTest::qExec(&tc, argc, argv); \
}

so it must be out of any namespace, but because it is out of namespaces the class object need to be passed with the correct scope, namespace::class

Upvotes: 1

Related Questions