技術共有

(私的使用) gtest 単体テスト

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

gtest は、多くのプラットフォーム (Linux、Mac OS X、Windows、Cygwin など) で実行できる C++ テストを作成するための Google のフレームワークです。 xUnit アーキテクチャに基づいています。自動テスト識別、豊富なアサーション、アサーションのカスタマイズ、停止テスト、非終了障害、XML レポート生成など、多くの便利な機能をサポートしています。

1.gtestの利点

2. テストフレームワークを構築する

gtest ダウンロードアドレス:GitHub - google/googletest: GoogleTest - Google テストおよびモック フレームワーク

ダウンロード方法は次のとおりです。git クローンGitHub - google/googletest: GoogleTest - Google テストおよびモック フレームワーク

インストール方法は次のとおりです。

$ cd googletest

注: make プロセス中にエラーが報告された場合は、CMakeLists.txt に次の行を追加し、コマンド SET(CMAKE_CXX_FLAGS "-std=c++11") を実行できます。

$ cmake 。

$ 作る

次に、lib ディレクトリに次のように生成されます: libgmock.a libgmock_main.a libgtest.a libgtest_main.a

最後に sudo make install します。

テストデモ

ディレクトリ構造:

テストする 2 つの機能は次のとおりです。

シンプル1.cc

  1. #include "sample1.h"
  2. // Returns n! (the factorial of n). For negative n, n! is defined to be 1.
  3. int Factorial(int n) {
  4. int result = 1;
  5. for (int i = 1; i <= n; i++) {
  6. result *= i;
  7. }
  8. return result;
  9. }
  10. // Returns true iff n is a prime number.
  11. bool IsPrime(int n) {
  12. // Trivial case 1: small numbers
  13. if (n <= 1) return false;
  14. // Trivial case 2: even numbers
  15. if (n % 2 == 0) return n == 2;
  16. // Now, we have that n is odd and n >= 3.
  17. // Try to divide n by every odd number i, starting from 3
  18. for (int i = 3; ; i += 2) {
  19. // We only have to try i up to the square root of n
  20. if (i > n/i) break;
  21. // Now, we have i <= n/i < n.
  22. // If n is divisible by i, n is not prime.
  23. if (n % i == 0) return false;
  24. }
  25. // n has no integer factor in the range (1, n), and thus is prime.
  26. return true;
  27. }

ヘッダーファイルsample.h

  1. #ifndef GTEST_SAMPLES_SAMPLE1_H_
  2. #define GTEST_SAMPLES_SAMPLE1_H_
  3. // Returns n! (the factorial of n). For negative n, n! is defined to be 1.
  4. int Factorial(int n);
  5. // Returns true iff n is a prime number.
  6. bool IsPrime(int n);
  7. #endif
テストクラスを書く

1. ヘッダー ファイル gtest/gtest.h、limits.h (最大 int と最小 int) が含まれます。

2.フォーマット

  1. TEST(测试套名,测试用例名)
  2. {
  3. //EXPECT_XX
  4. }

テスト スイートの下に複数のテスト ケースを含めることができます

例えば:

シンプルなユニットテスト.cc

  1. #include <limits.h>//包含最大最小数
  2. #include "sample1.h"
  3. #include "gtest/gtest.h"//gtest头文件
  4. namespace {
  5. TEST(FactorialTest, Negative) {
  6. // 测试名叫Negative,属于FactorialTest测试套
  7. // 测试用例
  8. EXPECT_EQ(1, Factorial(-5));//断言相等
  9. EXPECT_EQ(1, Factorial(-1));
  10. //前后顺序不影响
  11. EXPECT_GT(Factorial(-10), 0);//断言大于
  12. }
  13. TEST(FactorialTest, Zero) {
  14. EXPECT_EQ(1, Factorial(0));
  15. }
  16. TEST(FactorialTest, Positive) {
  17. EXPECT_EQ(1, Factorial(1));
  18. EXPECT_EQ(2, Factorial(2));
  19. EXPECT_EQ(6, Factorial(3));
  20. EXPECT_EQ(40320, Factorial(8));
  21. }
  22. // Tests IsPrime() 测试是否是素数
  23. TEST(IsPrimeTest, Negative) {
  24. //预测 true or false
  25. EXPECT_FALSE(IsPrime(-1));
  26. EXPECT_FALSE(IsPrime(-2));
  27. EXPECT_FALSE(IsPrime(INT_MIN));
  28. }
  29. TEST(IsPrimeTest, Trivial) {
  30. EXPECT_FALSE(IsPrime(0));
  31. EXPECT_FALSE(IsPrime(1));
  32. EXPECT_TRUE(IsPrime(2));
  33. EXPECT_TRUE(IsPrime(3));
  34. }
  35. TEST(IsPrimeTest, Positive) {
  36. EXPECT_FALSE(IsPrime(4));
  37. EXPECT_TRUE(IsPrime(5));
  38. EXPECT_FALSE(IsPrime(6));
  39. EXPECT_TRUE(IsPrime(23));
  40. }
  41. } // namespace

テストを実施する
メインなし

 テストの main 関数を実装するには、もちろん main 関数を記述する必要はありません。その後、gtest_main.a ライブラリに接続する必要があります (コンパイル時に -lgtest_main オプションを追加します)。たとえば、次のようにコンパイルします。

g++ サンプル1.cc サンプル1_unittest.cc -lgtest -std=c++14 -lgtest_main -lpthread -o test1

結果:

メイン付き

書き方を修正、最後にmainを追加

  1. int main(int argc, char** argv)
  2. {
  3. testing::InitGoogleTest(&argc,argv);
  4. return RUN_ALL_TESTS();
  5. }

test.cc を完成させます:

  1. #include <limits.h>//包含最大最小数
  2. #include "sample1.h"
  3. #include "gtest/gtest.h"//gtest头文件
  4. namespace {
  5. TEST(FactorialTest, Negative) {
  6. // 测试名叫Negative,属于FactorialTest测试套
  7. // 测试用例
  8. EXPECT_EQ(1, Factorial(-5));//断言相等
  9. EXPECT_EQ(1, Factorial(-1));
  10. //前后顺序不影响
  11. EXPECT_GT(Factorial(-10), 0);//断言大于
  12. }
  13. TEST(FactorialTest, Zero) {
  14. EXPECT_EQ(1, Factorial(0));
  15. }
  16. TEST(FactorialTest, Positive) {
  17. EXPECT_EQ(1, Factorial(1));
  18. EXPECT_EQ(2, Factorial(2));
  19. EXPECT_EQ(6, Factorial(3));
  20. EXPECT_EQ(40320, Factorial(8));
  21. }
  22. // Tests IsPrime() 测试是否是素数
  23. TEST(IsPrimeTest, Negative) {
  24. //预测 true or false
  25. EXPECT_FALSE(IsPrime(-1));
  26. EXPECT_FALSE(IsPrime(-2));
  27. EXPECT_FALSE(IsPrime(INT_MIN));
  28. }
  29. TEST(IsPrimeTest, Trivial) {
  30. EXPECT_FALSE(IsPrime(0));
  31. EXPECT_FALSE(IsPrime(1));
  32. EXPECT_TRUE(IsPrime(2));
  33. EXPECT_TRUE(IsPrime(3));
  34. }
  35. TEST(IsPrimeTest, Positive) {
  36. EXPECT_FALSE(IsPrime(4));
  37. EXPECT_TRUE(IsPrime(5));
  38. EXPECT_FALSE(IsPrime(6));
  39. EXPECT_TRUE(IsPrime(23));
  40. }
  41. } // namespace
  42. int main(int argc, char** argv)
  43. {
  44. testing::InitGoogleTest(&argc,argv);
  45. return RUN_ALL_TESTS();
  46. }

コンパイル時に -lgtest_main オプションを削除します。