Testing Database with QTest

  • Hello,

    I would like to write some QTest for database class, like connecting to database, insert tables , data.... I am beginner in this field and don't have a clue how should I start with it. I read some articles which mostly were about testing GUI. But they did not help me so far. I appreciate someone who could give me further information.

  • I would recomend you have a look at -> voidrealms.com

  • Suths thanks for the suggestion.
    But I still could not find QTest example. I know how to connect to database and get the query from but I don't know how am I supposed to write some QTest(UnitTest) for it. :(

  • Did u read chapter 16: Unit Testing in the Book: Foundations of Qt Development ?

  • Not related to databases, but the same type of concepts apply to testing as with anything else. Take a look at the tests under


  • Thank you MuhamedAuda for introducing the Book. I have read the UnitTest chapter. The first test I wrote is as following:

    void TestDBMethods::testConnection(){

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    QCOMPARE(db.isOpen(), true);


    in the normal case my connection will be established but in this case I got fail by QCOMPARE. Could anyone tell me where my error is?

    Question: what are the alternatives for testing the methods of another subproject in my test project?

  • Ok I got my error.

    I have to write @QCOMPARE(db.open(), true)@

    But I still am looking forward to any suggestions that may help me to test the methoes of other subprojects in my test projects.


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.