Qt:从多个测试类运行单元测试并总结所有测试类的输出 [英] Qt: run unit tests from multiple test classes and summarize the output from all of them

查看:34
本文介绍了Qt:从多个测试类运行单元测试并总结所有测试类的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt 自带QTest,还有一些文档:例如,官方教程.

Qt comes with QTest, and there are some docs: for example, an official tutorial.

然而,QTest 鼓励您将单元测试组织为单独的可执行文件.有一个特殊的宏,它生成 main(): QTEST_MAIN()

However, QTest encourages you to organize unit tests as separate executables. There is special macro for this, that generates main(): QTEST_MAIN()

老实说,我真的不喜欢这种方法:通常,一次运行所有测试会更有用,以确保最近的更改没有破坏任何东西.有时,屏蔽一些测试或执行一些单独的测试很有用,但这是一个例外,而不是规则.

To be honest, I really dislike this approach: generally, it is much more useful to run all tests at once, in order to make sure that recent changes haven't broken anything. Sometimes, it is useful to mask out some test or execute some individual test, but this is an exception, not the rule.

所以,我想一次运行所有测试.好的,我可以编写自己的 main() 来执行我想要的所有测试,例如:

So, I want to run all the tests at once. Ok, I can write my own main() that executes all tests I want, say, like this:

int main(int argc, char **argv)
{
   int status = 0;

   //-- run all tests
   {
      TestHTCodecISO14230 tc;
      status |= QTest::qExec(&tc, argc, argv);
   }

   {
      TestHTDataMsg tc;
      status |= QTest::qExec(&tc, argc, argv);
   }

   return status;
}

它确实运行了所有测试,但问题是我没有方便的所有测试摘要.比如说,对于上面的两个测试,我有两个单独的总结:

And it does run all tests, but the problem is that I don't have convenient summary of all tests. Say, for the two tests above, I have two separate summaries:

********* Start testing of TestHTCodecISO14230 *********
Config: Using QtTest library 5.4.1, Qt 5.4.1 (i386-little_endian-ilp32 shared (dynamic) release build; by GCC 4.6.1)
PASS   : TestHTCodecISO14230::initTestCase()
PASS   : TestHTCodecISO14230::decode_summary()
PASS   : TestHTCodecISO14230::encode()
PASS   : TestHTCodecISO14230::decode_encoded()
PASS   : TestHTCodecISO14230::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestHTCodecISO14230 *********
********* Start testing of TestHTDataMsg *********
Config: Using QtTest library 5.4.1, Qt 5.4.1 (i386-little_endian-ilp32 shared (dynamic) release build; by GCC 4.6.1)
PASS   : TestHTDataMsg::initTestCase()
PASS   : TestHTDataMsg::test1()
PASS   : TestHTDataMsg::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of TestHTDataMsg *********

返回的 status 在出错的情况下将为非零的事实肯定会有所帮助,但如果我也有摘要,则会更有帮助:

The fact that returned status will be non-zero in case of error is surely helpful, but is would be much more helpful if I have summary as well:

Totals: 8 passed, 0 failed, 0 skipped, 0 blacklisted

从我看来,这是不可能的:我找不到以编程方式获取通过、失败、跳过和列入黑名单的测试数量的方法:qExec() 只是 qExec() 中的一个函数code>QTest 命名空间,因此,在它执行后不可能收集到一些额外的信息.

From what I see, it is impossible: I can't find the way to programmatically get number of passed, failed, skipped and blacklisted tests: qExec() is just a function in the QTest namespace, so, it is impossible to gather some additional info after it executes.

好吧,可以解析输出字符串,但是,呃...

Well, it is possible to parse the output string, but, ugh...

在我看来,它的设计很糟糕.将 QTest 作为一个类可能会好得多,然后创建它的实例并向它提供一些测试类.然后,可以从实例中收集一些额外的信息.

To me, it looks like poor design. It would be probably much better to make QTest as a class, then make instance of it and feed some test classes to it. Then, some additional information could be gathered from an instance.

或者,也许我错过了什么.

Or, maybe I've missed something.

那么,问题是:QTest 是否有可能获得所有单元测试类的摘要输出?

So, the question is: is it possible with QTest to have summary output of all unit test classes?

推荐答案

正如我在评论中所写,我将按以下方式构建我的测试类:

As I wrote in my comment, I would construct my test classes in the following way:

class MyTests: public QObject
{
    Q_OBJECT
public:
    MyTests() : m_executed(0), m_failed(0)
private slots:
    [..]
    // This function will be called after each test
    void cleanup()
    {
        m_executed++;
        if (currentTestFailed()) {
            m_failed++;
        }        
    }

    // Output the summary of the test execution.
    void report() const
    {
        qDebug() << "Totals:"
                 << m_executed - m_failed  << "passed,"
                 << m_failed << "failed";
    }
private:
    int m_executed;
    int m_failed;
};

如果你有多个MyTests类的实例,你可以扩展它的API并总结执行结果产生全局测试执行报告.只需使用 C++ 类的全部优势即可.

If you have multiple instances of MyTests class, you can extend its API and sum up the execution results producing the global test execution report. Just use the whole strength of C++ classes.

这篇关于Qt:从多个测试类运行单元测试并总结所有测试类的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆