البرمجة

تشغيل اختبارات Qt متعددة

To run multiple QTest classes properly and get a summary for all the test classes, you can create a test suite that includes all your test classes. This way, you can run the test suite once and get a summary for all the tests. Here’s how you can do it:

  1. Create a new test suite class that includes all your test classes. You can use QList to store pointers to your test classes and then pass this list to QTest::qExec().

  2. Modify your main.cpp to create an instance of the test suite class and pass it to QTest::qExec().

Here’s an example of how you can do it:

cpp
#include #include #include "test_foo.h" #include "test_bar.h" // Assuming test_bar.h contains your TestBarClass class TestSuite : public QObject { Q_OBJECT public: TestSuite(QObject* parent = nullptr) : QObject(parent) { // Add your test classes to the suite m_testClasses.append(new TestFooClass()); m_testClasses.append(new TestBarClass()); // Add more test classes as needed } private: QList m_testClasses; private slots: void testSuite() { // Run all the test classes for (QObject* testClass : m_testClasses) { QTest::qExec(testClass); } } }; int main(int argc, char** argv) { QCoreApplication app(argc, argv); TestSuite testSuite; return QTest::qExec(&testSuite, argc, argv); }

In this example, TestSuite is a new test suite class that includes TestFooClass and TestBarClass. You can add more test classes to TestSuite as needed. The testSuite() slot runs all the test classes in the suite using QTest::qExec(). Finally, in main(), an instance of TestSuite is created and passed to QTest::qExec() to run all the tests.

By running this modified test application, you should see a summary for all the test classes at the end of the output, which should help you quickly identify if any tests have failed.

المزيد من المعلومات

إذا كنت تستخدم نسخة حديثة من Qt (مثل Qt 5.14+)، يمكنك استخدام QTEST_ADD_TEST لإضافة الاختبارات إلى الدليل الرئيسي. على سبيل المثال، يمكنك تعديل ملف main.cpp كما يلي:

cpp
#include #include #include "test_foo.h" #include "test_bar.h" int main(int argc, char** argv) { QCoreApplication app(argc, argv); QTest::qExec(new TestFooClass, argc, argv); QTest::qExec(new TestBarClass, argc, argv); return 0; }

هذا يقوم بإنشاء كائنات من TestFooClass و TestBarClass ويمررها إلى QTest::qExec() لتنفيذ الاختبارات. يُعتبر هذا النهج بسيطًا ولكنه لا يقدم ملخصًا لجميع الاختبارات كما هو موضح في السؤال الأصلي.

بالإضافة إلى ذلك، يمكنك استخدام Qt Test المتقدمة والمعروفة باسم “Testlib”. يمكنك إنشاء ملف .pro خاص بالاختبارات يحتوي على جميع ملفات الاختبارات الفرعية الخاصة بك وتضمينها في ملف main.cpp الخاص بك. على سبيل المثال:

pro
TEMPLATE = app TARGET = mytests QT += testlib CONFIG += console CONFIG -= app_bundle SOURCES += \ test_foo.cpp \ test_bar.cpp \ main.cpp

ثم يمكنك تعديل main.cpp ليكون كالتالي:

cpp
#include #include int main(int argc, char** argv) { QCoreApplication app(argc, argv); int status = 0; { TestFooClass testFoo; status |= QTest::qExec(&testFoo, argc, argv); } { TestBarClass testBar; status |= QTest::qExec(&testBar, argc, argv); } return status; }

بهذه الطريقة، يتم تنفيذ اختباراتك ويتم تجميع ملخص النتائج في نقطة واحدة.

زر الذهاب إلى الأعلى