2

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?

Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21
Yeet
  • 92
  • 7
  • 1
    Whether you want to use namespaces in your project or not doesn't really have anything to do with Qt or QTest. It should work equally well with or without namespaces. – lubgr Aug 29 '19 at 13:53
  • 1
    What was not working? Have you adapted the macro call like that: `QTEST_MAIN(MyNamespace::TestQString)`? – Benjamin Bihler Aug 29 '19 at 13:59

1 Answers1

1

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

Moia
  • 2,216
  • 1
  • 12
  • 34
  • this is what I tried other than the APPLESS_Main. For whatever reason, the tests wouldn't run while it was inside a custom namespace so I removed the namespace and it worked – Yeet Aug 29 '19 at 14:11
  • @Yeet you need to show the code that didnt work (along with the error messages) – 463035818_is_not_an_ai Aug 29 '19 at 14:12
  • @Yeet my code compile and run properly. You can see it immediatly on QtCreator in the "Tests" panel if it' correctly parsed. So or you're missing something or you did some other error in your code. – Moia Aug 29 '19 at 14:16
  • 1
    @Yeet clean the project being sure of all the moc and .o have been deleted, then run qmake and build again – Moia Aug 29 '19 at 14:23
  • I am actually using scons so the process is a little different but thanks I'll try that – Yeet Aug 29 '19 at 14:24
  • @Yeet every time you change something involving the moc generation the project could not compile unless you don't clean the files. This has to be your issue because the code above do work. Eventually when you verified that remember to mark the answer as accepted please. – Moia Aug 29 '19 at 14:31