当前位置:网站首页>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
边栏推荐
- @Detailed introduction of configuration annotation
- Is it safe to buy funds online?
- SQL uses the in keyword to query multiple fields
- Get started with mongodb
- Orthodontic precautions (continuously updated)
- 数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
- Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
- redis缓存工具类,值得拥有~
- Kubectl's handy command line tool: Oh my Zsh tips and tricks
- BSS 7230 航空内饰材料阻燃性能测试
猜你喜欢
一鍵免費翻譯300多頁的pdf文檔
How did a fake offer steal $540million from "axie infinity"?
Idea automatically generates serialVersionUID
Les mots ont été écrits, la fonction est vraiment puissante!
At the age of 35, I made a decision to face unemployment
Pypharm uses, and the third-party library has errors due to version problems
Learn about scratch
关于CH32库函数与STM32库函数的区别
The function is really powerful!
Flash download setup
随机推荐
webflux - webclient Connect reset by peer Error
C - linear table
Solutions to problems in sqlserver deleting data in tables
平衡二叉树【AVL树】——插入、删除
[leetcode] 20. Valid brackets
Connect diodes in series to improve voltage withstand
Take you hand in hand to build Eureka client with idea
Database interview questions + analysis
Basic learning of SQL Server -- creating databases and tables with the mouse
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
How did a fake offer steal $540million from "axie infinity"?
35岁那年,我做了一个面临失业的决定
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
C - minute number V3
Display the server hard disk image to the browser through Servlet
Basic learning of SQL Server -- creating databases and tables with code
【路径规划】使用垂距限值法与贝塞尔优化A星路径
ASP. Net query implementation
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
HDU - 1260 Tickets(线性DP)