informasi kontak saya
Surat[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
gtest adalah framework dari Google untuk menulis pengujian C++ yang dapat dijalankan di banyak platform (termasuk Linux, Mac OS X, Windows, Cygwin, dll.). Berdasarkan arsitektur xUnit. Ini mendukung banyak fitur berguna, termasuk identifikasi pengujian otomatis, pernyataan kaya, penyesuaian pernyataan, pengujian kematian, kegagalan non-terminasi, pembuatan laporan XML, dll.
alamat unduhan gtest:GitHub - google/googletest: GoogleTest - Kerangka Pengujian dan Mocking Google
Cara pengunduhannya adalah:klon gitGitHub - google/googletest: GoogleTest - Kerangka Pengujian dan Mocking Google
Metode instalasinya adalah:
$ cd uji google
Catatan: Jika kesalahan dilaporkan selama proses pembuatan, Anda dapat menambahkan baris berikut ke CMakeLists.txt, lalu jalankan perintah berikut: SET(CMAKE_CXX_FLAGS "-std=c++11")
$ cmake .
$ membuat
Kemudian akan dihasilkan di direktori lib: libgmock.a libgmock_main.a libgtest.a libgtest_main.a
Akhirnya kami sudo make install.
Struktur Direktori:
Dua fungsi yang akan diuji adalah sebagai berikut:
simple1.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;
- }
Contoh file header.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. Berisi file header gtest/gtest.h, limit.h (int maksimum dan int minimum)
2.Format
- TEST(测试套名,测试用例名)
- {
- //EXPECT_XX
-
- }
Mungkin ada beberapa kasus pengujian dalam satu rangkaian pengujian
misalnya:
uji_unit_sederhana.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
Untuk mengimplementasikan fungsi utama pengujian, tentunya kita tidak perlu menulis fungsi utama, kemudian kita perlu terhubung ke perpustakaan gtest_main.a (tambahkan opsi -lgtest_main saat kompilasi). Misalnya, kompilasi seperti ini:
g++ contoh1.cc contoh1_unittest.cc -lgtest -std=c++14 -lgtest_main -lpthread -o test1
Hasil dari:
Memperbaiki metode penulisan, tambahkan main di akhir
- int main(int argc, char** argv)
- {
- testing::InitGoogleTest(&argc,argv);
- return RUN_ALL_TESTS();
- }
Tes lengkap.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();
- }
Hapus opsi -lgtest_main saat kompilasi: