簡體   English   中英

使用測試套件,如何讓測試顯示在 Qt5 的“測試”窗格視圖中?

[英]Using a test suite, how can I get the tests to show up in the "Tests" pane view for Qt5?

我基本上按照此處此處的說明在 Qt5 中設置了 TestSuite。 它按描述工作; 然而; 當我從項目視圖,以測試切換查看它顯示各個類的測試如下面的TestsView圖像英寸我希望它顯示我的測試類和各個函數槽。 這在我調試或我只想在測試類中執行單個測試函數時很有用。

我發現的解決方法解決方案:在 main.cpp 中,如果我實例化類並調用 qExec() 而不是使用 QOject* 到測試類實例,則它可以工作(顯示在下面的 main.cpp 中); 然而,這違背了測試套件類的目的。

測試視圖

// testsuite.h
#pragma once

// Qt headers
#include <QObject>
#include <QtTest/QtTest>

class TestSuite : public QObject
{
    Q_OBJECT

public:
    explicit TestSuite();
    virtual ~TestSuite();

    static QVector<QObject*>& suite();
};

// testsuite.cpp
#include "testsuite.h"

#include <QDebug>

TestSuite::TestSuite()
{
    suite().push_back(this);
}

TestSuite::~TestSuite() {}

QVector<QObject*>& TestSuite::suite()
{
    static QVector<QObject*> instance;
    return instance;
}
// main.cpp
#include "testsuite.h"

#include <QtTest>

int main(int argc, char* argv[])
{
    Q_UNUSED(argc)
    Q_UNUSED(argv)

    int failedTestsCount = 0;

    for (auto &test : TestSuite::suite()) {
        int result = QTest::qExec(test);
        if (result != 0) {
            failedTestsCount++;
        }
    }

    // Work around w/ #include class file
    //TestExampleClass testExampleClass ;
    //QTest::qExec(&testExampleClass );

    return failedTestsCount;
}
// testexampleclass.h
#include <QtTest/QtTest>

#include "testsuite.h" 

class TestExampleClass : public TestSuite
{
      Q_OBJECT

   private slots:
      void  test_addSomeStuff();
};
// testexampleclass.cpp
#include "testexampleclass.h"

static TestExampleClass  sInstance;

// test adding list of numbers
void  TestExampleClass::test_addSomeStuff()
{
   QVERIFY( true );
}

編輯:我使用 Qt Creator 4.1.2 和 Qt 5.13.2 (MSVC 2017)

我有同樣的問題。 在“重新掃描 Sests”期間,QC 搜索以下條目:{"QTEST_MAIN", "QTEST_APPLESS_MAIN", "QTEST_GUILESS_MAIN"} 以下虛擬宏代碼有幫助:

#ifdef QTEST_MAIN
#undef QTEST_MAIN
#endif
#define QTEST_MAIN(TestObject)

比您有 o 將QTEST_MAIN(TestClassName)放入您的測試類聲明中。 之后我的測試在 Treeview 中可見: Qt Creator Tests

但是 QTEST_MAIN 將不再起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM