当前位置:网站首页>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
边栏推荐
- P2141 [noip2014 popularization group] abacus mental arithmetic test
- 二叉排序树【BST】——创建、查找、删除、输出
- Take you hand in hand to build Eureka server with idea
- Display the server hard disk image to the browser through Servlet
- [summary] some panels and videos seen
- C语言学习
- Orthodontic precautions (continuously updated)
- How did a fake offer steal $540million from "axie infinity"?
- Resolve the URL of token
- Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
猜你喜欢
蓝桥ROS中使用fishros一键安装
Take you hand in hand to build Eureka server with idea
一份假Offer如何盗走了「Axie infinity」5.4亿美元?
BSS 7230 航空内饰材料阻燃性能测试
Traduction gratuite en un clic de plus de 300 pages de documents PDF
Take you hand in hand to build feign with idea
Navicat connects Oracle
用語雀寫文章了,功能真心强大!
UIC564-2 附录4 –阻燃防火测试:火焰的扩散
Archery installation test
随机推荐
10 schemes to ensure interface data security
Alibaba cloud MySQL cannot connect
Aitm3.0005 smoke toxicity test
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
Is it safe for tongdaxin to buy funds?
2022.7.7-----leetcode. six hundred and forty-eight
[leetcode] 20. Valid brackets
一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)
Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
Chisel tutorial - 04 Control flow in chisel
SQL uses the in keyword to query multiple fields
About the difference between ch32 library function and STM32 library function
快速回复二极管整流特性
Pypharm uses, and the third-party library has errors due to version problems
Traduction gratuite en un clic de plus de 300 pages de documents PDF
redis缓存工具类,值得拥有~
C - minute number V3
Chisel tutorial - 01 Introduction to Scala