Qt 5.9.1 参考手册 QtTest 第1章 写一个单元测试

本文介绍如何使用Qt Test框架为QString类编写和执行简单的单元测试。通过实例演示了测试类的结构、测试函数的编写及宏函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Qt 5.9.1 Reference Documentation


Chapter 1: Writing a Unit Test 

In this first chapter we will see how to write a simple unit test for a class, and how to execute it. 

在这一章节我们将会看到如何为一个类写一个简单的单元测试并执行它。


Writing a Test
Let's assume you want to test the behavior of our QString class. First, you need a class that contains your test functions. This class has to inherit from QObject:
假设你想要测试QString类的行为。首先,你需要一个包含你的测试函数的类。这个类需要继承自QObject。

  #include <QtTest/QtTest>


  class TestQString: public QObject
  {
      Q_OBJECT
  private slots:
      void toUpper();
  };


Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it.
Then you need to implement the test function itself. The implementation could look like this:
注意:你需要引入QTest头文件,并将测试函数声明为私有槽函数,这样测试框架会找到这些函数并执行。然后,你需要实现这些测试函数。一个简单的例子如下:

  void TestQString::toUpper()
  {
      QString str = "Hello";
      QVERIFY(str.toUpper() == "HELLO");
  }


The QVERIFY() macro evaluates the expression passed as its argument. If the expression evaluates to true, the execution of the test function continues. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.
But if you want a more verbose output to the test log, you should use the QCOMPARE() macro instead:

QVERIFY宏函数可以比较一个表达式的结果是否为真。如果为真,测试函数将会继续,否则会添加一条消息描述失败情况到日志中,然后测试函数会被终止。

如果需要详尽的比较信息可以用QCOMPARE。


  void TestQString::toUpper()
  {
      QString str = "Hello";
      QCOMPARE(str.toUpper(), QString("HELLO"));
  }


If the strings are not equal, the contents of both strings are appended to the test log, making it immediately visible why the comparison failed.
Finally, to make our test case a stand-alone executable, the following two lines are needed:
如果2个字符串不相同,QCOMPARE会将2个值均打印出来。最后,要让我们的测试用例单独执行,需要添加以下2行:

  QTEST_MAIN(TestQString)
  #include "testqstring.moc"


The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work. 

QTEST_MAIN() 宏函数简单地扩展了一个main函数,并在main函数中运行所有的测试函数。注意如果声明和实现都放在一个cpp文件时,需要包括.moc文件令Qt的内建机制工作。

Executing a Test

Now that we finished writing our test, we want to execute it. Assuming that our test was saved as testqstring.cpp in an empty directory, we build the test using qmake to create a project and generate a makefile.

现在我们完成了单元测试的代码编写工作,我们希望执行它。假设我们的测试文件名为testqstring.cpp并位于一个空文件夹下,我们可以用qmake来创建一个工程然后产生makefile,用这个makefile来构建我们的测试。



  /myTestDirectory$ qmake -project "QT += testlib"
  /myTestDirectory$ qmake
  /myTestDirectory$ make


Note: If you're using windows, replace make with nmake or whatever build tool you use.
Running the resulting executable should give you the following output:
注意:如果你使用windows,用nmake或者其他构建工具来代替make。运行测试项目对应可执行文件将会产生如下输出:

  ********* Start testing of TestQString *********
  Config: Using QtTest library %VERSION%, Qt %VERSION%
  PASS   : TestQString::initTestCase()
  PASS   : TestQString::toUpper()
  PASS   : TestQString::cleanupTestCase()
  Totals: 3 passed, 0 failed, 0 skipped
  ********* Finished testing of TestQString *********


Congratulations! You just wrote and executed your first unit test using the Qt Test framework.
Files:
tutorial1/testqstring.cpp
tutorial1/tutorial1.pro
//qt单元测试用法,qt测试例子,qt单元测试demo,qt单元测试简单例子,qt单元测试例程,qt单元测试简单例子, qt5单元测试例子,qt5单元测试代码,qt5单元测试工程例子,测试运行ok //首先,用标准的qt测试单元导向,建立一个变准的qt单元测试程序框架,建立话好,能编译通过并且执行的。 float tst_untitledtest::calculate_area_of_circle(float r)//计算圆的面积的函数 { return r*r*3.1415926; } void tst_untitledtest::testA() { QFETCH(float,inputR);//QFETCH用户获取testA_data的数据,inputR是浮点型的。 QFETCH(float,resltArea);//resltArea是浮点型的,这两个参数都是在testA_data里面定义的 //QCOMPARE(input,rslt); QCOMPARE(calculate_area_of_circle(inputR), resltArea);//测试结果比较,用宏QCOMPARE。可以直接调用inputR和resltArea。 qDebug()<<"============test ok=============="; } void tst_untitledtest::testA_data() { QTest::addColumn ("inputR");//定义测试数据,添加测试数据,添加一列,此列是输入 QTest::addColumn ("resltArea");//定义测试数据,添加测试数据,添加测试数据,定义一列,是结果 QTest::newRow("a")<<1.0f<<3.14159f;//添加一行测试数据,每行就是每一次测试,对应上面定义的input和result QTest::newRow("b")<<2.0f<<5.0f;//添加一行测试数据,每行就是每一次测试,对应上面定义的input和result QTest::newRow("c")<<3.0f<<6.0f;//添加一行测试数据,每行就是每一次测试,对应上面定义的input和result,所以这是3次第是,3 row } /** ——————————— | inputR | resultArea| __________________________ | a | 1.0f | 5.0f | __________________________ | b | 2.0f | 5.0f | __________________________ | c | 3.0f | 6.0f | -------------------------- **/ 运行结果: ********* Start testing of tst_untitledtest ********* Config: Using QtTest library 5.5.1, Qt 5.5.1 (x86_64-little_endian-llp64 shared (dynamic) debug build; by MSVC 2013) PASS : tst_untitledtest::initTestCase() QDEBUG : tst_untitledtest::testA(a) ============test ok============== PASS : tst_untitledtest::testA(a) FAIL! : tst_untitledtest::testA(b) Compared floats are not the same (fuzzy compare) Actual (calculate_area_of_circle(inputR)): 12.5664 Expected (resltArea) : 5 tst_tst_untitledtest.cpp(36) : failure location FAIL! : tst_untitledtest::testA(c) Compared floats are not the same (fuzzy compare) Actual (calculate_area_of_circle(inputR)): 28.2743 Expected (resltArea) : 6 tst_tst_untitledtest.cpp(36) : failure location PASS : tst_untitledtest::cleanupTestCase() Totals: 3 passed, 2 failed, 0 skipped, 0 blacklisted ********* Finished testing of tst_untitledtest *********
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值