当前位置:网站首页>[googletest] googletest unit test (1) get started quickly
[googletest] googletest unit test (1) get started quickly
2022-07-22 02:26:00 【Half raw melon blog】
Quick start
GoogleTest yes Google A set of for writing C++ The framework for testing , It can run on multiple platforms ( Include Linux、MacOS X、Windows、Cygwin etc. ). be based on xUnit framework , Support many easy-to-use features , Including automatic identification test 、 Rich assertions 、 Assert custom 、 Death test 、 Non terminating failure 、 Generate XML Reports, etc .
hereinafter referred to as GTest.
GTest The advantages of
A good test should include the following features .
- The test should be independent 、 Repeatable . The result of one test should not be a prerequisite for another test .
- GTest Each test in runs in a separate object . If a test fails , You can debug it separately .
- The test should have a clear structure .
- GTest The test has a good organizational structure , Easy to maintain .
- Tests should be portable and reusable . A lot of code is platform independent , Therefore, their testing also needs to be platform independent .
- GTest Can operate on a variety of operating systems 、 Working under a variety of compilers , It has good portability .
- When the test fails , The information should be as detailed as possible .
- GTest In case of failure, do not stop the next test , You can also choose to use non terminating failures to continue the current test . In this way, you can test as many problems as possible at one time .
- The test framework should avoid allowing developers to maintain things related to the test framework .
- GTest All defined tests can be automatically identified , You don't need to list them all .
- The test should be fast enough .
- GTest On the premise of meeting the test independence , Allows you to reuse shared data , They only need to be created once .
- GTest It's using xUnit framework , You'll find out and JUnit、PyUnit Is very similar , So get started very quickly .
Build a test framework
- GTest Project address of :https://github.com/google/googletest
The installation process is as follows :
- git clone https://github.com/google/googletest.git
- cd googletest
- mkdir build
- cd build
- cmake …
- make
- sudo make install
Be careful :
- If make Report an error in the process , May be gcc Version is too low , From the official project issus We can get the value of ,gcc 4.8 Version no longer supports , As shown in the figure below .linux make[1]: *** [googletest/CMakeFiles/gtest.dir/all] Error 2#3639
- install gcc-5( Or later ),Ubuntu20.04 install gcc-5.
- Successfully compiled build .
- among ,/build/lib Will be generated in the directory :libgmock.a libgmock_main.a libgtest.a libgtest_main.a file .
Test examples
sample1.h
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_
// Factorial
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
// return n!(n The factorial ). For negative n,n! Defined as 1.
int Factorial(int n);
// Judge whether it is prime
// Returns true iff n is a prime number.
bool IsPrime(int n);
#endif
sample1.cc
#include "sample1.h"
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Returns true iff n is a prime number.
bool IsPrime(int n) {
// Trivial case 1: small numbers
if (n <= 1) return false;
// Trivial case 2: even numbers
if (n % 2 == 0) return n == 2;
// Now, we have that n is odd and n >= 3.
// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
// We only have to try i up to the square root of n
if (i > n/i) break;
// Now, we have i <= n/i < n.
// If n is divisible by i, n is not prime.
if (n % i == 0) return false;
}
// n has no integer factor in the range (1, n), and thus is prime.
return true;
}
sample1_unittest.cc
#include <limits.h>
#include "sample1.h"
#include <gtest/gtest.h>
namespace {
//TEST( Test Suite , Test case name )
TEST(FactorialTest, Negative) {
// Call the corresponding function , Whether the result is 1, Determine whether the test case passes
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
}
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}
TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
// Tests IsPrime()
TEST(IsPrimeTest, Negative) {
EXPECT_FALSE(IsPrime(-1));
EXPECT_FALSE(IsPrime(-2));
EXPECT_FALSE(IsPrime(INT_MIN));
}
TEST(IsPrimeTest, Trivial) {
EXPECT_FALSE(IsPrime(0));
EXPECT_FALSE(IsPrime(1));
EXPECT_TRUE(IsPrime(2));
EXPECT_TRUE(IsPrime(3));
}
TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(4));
EXPECT_TRUE(IsPrime(5));
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
} // namespace
compile
g++ sample1.cc sample1_unittest.cc -lgtest -std=c++14 -lgtest_main -lpthread -o test1
Be careful :
When this article was published ,GTest No longer supported C++11, The minimum supported version is C++14, therefore -std=c++11 Will report a mistake , Error resolution ( primary issues)——Commit e009c3d fails to build with "error: wrong number of template arguments (0, should be 1), take c++ The version is specified as c++14.( At first I thought it was me ubuntu The version of is too low , Specially changed the machine .)
above sample1_unittest.cc In the test procedure , We didn't write main function , Because we linked gtest_main This library .
When testing , Select representative use cases for testing .
perform
./test1
- All test cases pass
- next , Let's modify a code ( This modification judges a code in the prime number function , Return the original false The position of is changed to ture.), Recompile execution , Find the error .
Handwriting main function
int main(int argc, char** argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
compile : This can be removed at compile time gtest_main library .
g++ sample1.cc sample1_unittest.cc -lgtest -std=c++14 -lpthread -o test1
边栏推荐
猜你喜欢
随机推荐
SkyWalking自定义链路追踪、性能剖析
Flutter引入图形验证码,并保留SessionId
07.02 哈夫曼编码
Creation of configuration files in Nacos configuration center and reading of Nacos configuration center by microservices
Learn STM32, starting with the framework design of Hal library
VLAN与三层交换机
AtCoder Beginner Contest 260 - C, D, E
DHCP协议
Minor spanning tree
分布式事务之二阶段提交、AT模式、TCC模式
After 2 days of obsolescence, Microsoft gave up "banning" commercial open source!
Skywalking server building and microservice access to skywalking
云原生(九) | Devops篇之Jenkins安装与实战
Hcip day 8 notes
SkyWalking服务端的搭建、微服务接入SkyWalking
使用LoadBalancer替换Ribbon负载均衡器
un7.20:如何同时将关联表中的两个属性展示出来?
[natural language processing] pre training word vector Baidu cloud download [word2vec, glove, fasttext]
电脑是怎样上网的 (二) 从网线到网络设备
Static distribution and dynamic distribution in trust