当前位置:网站首页>Using Google test in QT
Using Google test in QT
2022-07-07 23:53:00 【Raring_ Ringtail】
I found a good book a few days ago 《The Ray Tracer Challenge》 This book is different from other books on programming or algorithms in that it does not provide runnable code , It only provides pseudo code and test samples . It requires the reader to follow the explanation and pseudo code in the book to complete a light tracing engine in the language familiar to the reader , And judge whether it has been completed correctly according to the test samples provided in the book .
The test tool used in the book is cucumber One BDD( Behavior driven development ) Tools , The book uses cucumber Of Gherkin Language as the description language of test cases in the book , This language uses almost natural language to describe test cases . At this point, readers may want to ask , Then why don't you cucumber Well ? The title doesn't say Google Test Do you ?
Because I didn't install , Or I can't use it . I mainly use c++ Programmed and cucumber Yes c++ My support is not good , I can hardly find relevant information . And java Different , of c and c++ There are few testing tools and materials , Maybe it's because c and c++ It's not that popular .
stay Qt Creator Use in Google Test
Here I will use an example to illustrate how to Qt Use in Google Test Conduct unit tests and how to set up automatic tests after compilation .
I recorded a video and walked through the whole process , You can see that :
Qt Creator Use in Google Test Unit test
Qt Creator Support four test frameworks , Namely :QtTest、QtQuickTest、Google Test、Boost Test
I used one of them QtTest and Google Test ,QtTest It's troublesome to use and Google Test Relatively simple and easy to use .
First we need to download Google Test , Go directly to its GitHub Just upload and download , The address is :https://github.com/google/googletest , After downloading, unzip it and put it in a directory you like .
Then we need to create a Subdirectory item engineering , Here's the picture :
Subdirectory item As the name suggests, this project is used to include other projects , That is, multiple projects can be included in this project .
Another window will pop up immediately after creation , Continue to ask for a new project ( Be careful The title becomes New subproject ):
For the convenience of demonstration , I create the simplest c++ Program :
Add to it demo.h
and demo.cpp
Add add Function declaration and definition :
Then add a test subproject :
choice Auto Test Project :
Choose to use Google Test :
Set up Test suite name and Test case name , The former is the name of your group of tests , The latter is the name of a test case in this group of tests , This can be modified later .
An important step here is to fill in Googletest Source directory address , Unzip the previous download Google Test Just fill in the catalogue of .
Well, here is the generated test project :
among main.cpp
China has already put Google Test The relevant code for initializing and running the test is written , We just need to create a new cpp
Just write a test sample file . The project will generate a test related header file by default , It contains an example of testing , You can imitate this example to write , But be careful not to write tests in the header file , To be in cpp
Written in a file .
Next, delete the automatically generated test case and create a demoTest.cpp
,
stay demoTest.cpp
Contains automatically generated header files tst_add.h
And then you can go in demoTest.cpp
Write test samples in , But the test written in this way cannot test the one written above add
Functional , We need to take App engineering Medium demo.h
and demo.cpp
Add to Test engineering in :
Add the corresponding path to the project configuration file as shown in the figure Test.pro Then you can .
Then you can write test samples :
Here I write a correct test and a wrong test . We can click... In the toolbar below Test Result Or press this button Alt+8 Switch to Test result Click the green triangle running symbol on the interface to run the test :
You can see that it indicates that the second test failed ,1+2 Originally equal to 3 The correct value of my test is 4, So the above figure suggests add(1,2) The value of is 3 Actually, it should be 4.
Google Test Some common assertions of can be found in its documentation , Here I list several common assertions :
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE(condition); | EXPECT_TRUE(condition); | condition is true |
ASSERT_FALSE(condition); | EXPECT_FALSE(condition); | condition is false |
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_EQ(val1, val2); | EXPECT_EQ(val1, val2); | val1 == val2 |
ASSERT_NE(val1, val2); | EXPECT_NE(val1, val2); | val1 != val2 |
ASSERT_LT(val1, val2); | EXPECT_LT(val1, val2); | val1 < val2 |
ASSERT_LE(val1, val2); | EXPECT_LE(val1, val2); | val1 <= val2 |
ASSERT_GT(val1, val2); | EXPECT_GT(val1, val2); | val1 > val2 |
ASSERT_GE(val1, val2); | EXPECT_GE(val1, val2); | val1 >= val2 |
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_STREQ(str1,str2); | EXPECT_STREQ(str1,str2); | the two C strings have the same content |
ASSERT_STRNE(str1,str2); | EXPECT_STRNE(str1,str2); | the two C strings have different contents |
ASSERT_STRCASEEQ(str1,str2); | EXPECT_STRCASEEQ(str1,str2); | the two C strings have the same content, ignoring case |
ASSERT_STRCASENE(str1,str2); | EXPECT_STRCASENE(str1,str2); | the two C strings have different contents, ignoring case |
For more relevant settings, please check Google Test Primer:https://github.com/google/googletest/blob/master/googletest/docs/primer.md
How to automatically run tests after program compilation
If the TDD Developing programs in a different way , We must hope that the program can automatically run through the test after each compilation . We can set the project , Click the item in the left sidebar , And then click Project Settings Medium Testing, And then on the right side Testing Under the big characters Global Change to Custom And select GTest, Finally, at the bottom Automatically run tests after build In the drop-down box of None Change to All or Selected That's all right. .
Welcome to my WeChat official account. Notes on Jiangda , And mine B Station account Notes on Jiangda
边栏推荐
- P1055 [noip2008 popularization group] ISBN number
- Possible SQL for Oracle table lookup information
- Apng2gif solutions to various problems
- c—线性表
- BSS 7230 航空内饰材料阻燃性能测试
- LinkedBlockingQueue源码分析-新增和删除
- 【leetcode】day1
- Problems faced when connecting to sqlserver after downloading (I)
- PostGIS learning
- Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
猜你喜欢
Get started with mongodb
Take you hand in hand to build Eureka client with idea
光流传感器初步测试:GL9306
受限线性表
Basic learning of SQL Server -- creating databases and tables with the mouse
ping报错:未知的名称或服务
Magic fast power
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
Archery installation test
An example analysis of MP4 file format parsing
随机推荐
95. (cesium chapter) cesium dynamic monomer-3d building (building)
数据湖(十五):Spark与Iceberg整合写操作
Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
mysql8.0 ubuntu20.4
BSS 7230 航空内饰材料阻燃性能测试
Come on, brother
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Access database query all tables SQL
Display the server hard disk image to the browser through Servlet
SQL uses the in keyword to query multiple fields
保证接口数据安全的10种方案
507 field D - extraterrestrial relics
May day d-light
Wechat applet development beginner 1
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
postgis学习
Gorm Association summary
ASP. Net query implementation
快速回复二极管整流特性
Ora-01741 and ora-01704