当前位置:网站首页>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 :

 Insert picture description here
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 ):

 Insert picture description here
For the convenience of demonstration , I create the simplest c++ Program :

 Insert picture description here

Add to it demo.h and demo.cpp Add add Function declaration and definition :

 Insert picture description here
 Insert picture description here

Then add a test subproject :

 Insert picture description here

choice Auto Test Project

 Insert picture description here

Choose to use Google Test
 Insert picture description here

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 .

 Insert picture description here

Well, here is the generated test project :

 Insert picture description here

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 .

 Insert picture description here

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 :

 Insert picture description here

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 :

 Insert picture description here

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 :

 Insert picture description here

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 assertionNonfatal assertionVerifies
ASSERT_TRUE(condition);EXPECT_TRUE(condition);condition is true
ASSERT_FALSE(condition);EXPECT_FALSE(condition);condition is false
Fatal assertionNonfatal assertionVerifies
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 assertionNonfatal assertionVerifies
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 Primerhttps://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. .

 Insert picture description here


Welcome to my WeChat official account. Notes on Jiangda , And mine B Station account Notes on Jiangda

原网站

版权声明
本文为[Raring_ Ringtail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130554125971.html