내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
gtest는 다양한 플랫폼(Linux, Mac OS X, Windows, Cygwin 등 포함)에서 실행할 수 있는 C++ 테스트를 작성하기 위한 Google의 프레임워크입니다. xUnit 아키텍처를 기반으로 합니다. 자동 테스트 식별, 풍부한 어설션, 어설션 사용자 정의, 종료 테스트, 종료되지 않는 실패, XML 보고서 생성 등을 포함한 많은 유용한 기능을 지원합니다.
gtest 다운로드 주소:GitHub - google/googletest: GoogleTest - Google 테스트 및 모킹 프레임워크
다운로드 방법은 다음과 같습니다.git 클론GitHub - google/googletest: GoogleTest - Google 테스트 및 모킹 프레임워크
설치 방법은 다음과 같습니다.
$ cd 구글테스트
참고: 만들기 프로세스 중에 오류가 보고되면 CMakeLists.txt에 다음 줄을 추가한 후 다음 명령을 실행할 수 있습니다. SET(CMAKE_CXX_FLAGS "-std=c++11")
영어: $ cmake .
$ 만들다
그러면 lib 디렉토리에 생성됩니다: libgmock.a libgmock_main.a libgtest.a libgtest_main.a
마지막으로 sudo make install을 실행합니다.
디렉토리 구조:
테스트할 두 가지 기능은 다음과 같습니다.
심플1.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;
- }
헤더 파일 샘플.h
- #ifndef GTEST_SAMPLES_SAMPLE1_H_
- #define GTEST_SAMPLES_SAMPLE1_H_
-
- // Returns n! (the factorial of n). For negative n, n! is defined to be 1.
- int Factorial(int n);
-
- // Returns true iff n is a prime number.
- bool IsPrime(int n);
-
- #endif
1. 헤더 파일 gtest/gtest.h,limits.h(최대 int 및 최소 int)를 포함합니다.
2.형식
- TEST(测试套名,测试用例名)
- {
- //EXPECT_XX
-
- }
테스트 스위트 아래에는 여러 테스트 케이스가 있을 수 있습니다.
예를 들어:
심플유닛테스트.cc
- #include <limits.h>//包含最大最小数
- #include "sample1.h"
- #include "gtest/gtest.h"//gtest头文件
- namespace {
-
- TEST(FactorialTest, Negative) {
- // 测试名叫Negative,属于FactorialTest测试套
- // 测试用例
- 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) {
- //预测 true or false
- 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
테스트의 기본 기능을 구현하려면 물론 기본 기능을 작성할 필요는 없으며 gtest_main.a 라이브러리에 연결해야 합니다(컴파일할 때 -lgtest_main 옵션 추가). 예를 들어 다음과 같이 컴파일합니다.
g++ 샘플1.cc 샘플1_유닛테스트.cc -lgtest -std=c++14 -lgtest_main -lpthread -o 테스트1
결과:
작성 방법 수정, 끝에 main 추가
- int main(int argc, char** argv)
- {
- testing::InitGoogleTest(&argc,argv);
- return RUN_ALL_TESTS();
- }
test.cc를 완료하세요.
- #include <limits.h>//包含最大最小数
- #include "sample1.h"
- #include "gtest/gtest.h"//gtest头文件
- namespace {
-
- TEST(FactorialTest, Negative) {
- // 测试名叫Negative,属于FactorialTest测试套
- // 测试用例
- 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) {
- //预测 true or false
- 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
-
- int main(int argc, char** argv)
- {
- testing::InitGoogleTest(&argc,argv);
- return RUN_ALL_TESTS();
- }
컴파일할 때 -lgtest_main 옵션을 제거하십시오.