[英]How do I set up Google Test with a GNU Make project?
由於 Google Test 網頁上基本上沒有文檔——我該怎么做? 到目前為止我所做的:
./configure && make
-Igtest/include
-Lgtest/lib
我寫了一個小樣本測試:
#include "gtest/gtest.h" int main(int argc, char **args) { return 0; } TEST(someTest,testOne) { ASSERT_EQ(5,5); }
這編譯得很好,但鏈接器似乎根本沒有被逗樂。 我收到了大量的錯誤消息
test/main.o: 在函數
someTest_testOne_Test::TestBody()': main.cpp:(.text+0x96): undefined reference to
testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
現在我忘了做什么?
最好的例子 Makefile 是與 Google Test 一起分發的。 它向您展示了如何根據您是要使用 Google 的main()
函數還是您自己的函數,將gtest_main.a
或gtest.a
與您的二進制文件鏈接gtest_main.a
。
在向項目生成文件添加內容之前,我喜歡弄清楚它實際運行的是哪些命令。 所以這里是我用來手動構建 sample1 單元測試的命令列表。
g++ -c -I../include sample1.cc
g++ -c -I../include sample1_unittest.cc
g++ -pthread -o s1_ut sample1.o sample1_unittest.o ../lib/.libs/libgtest.a ../lib/.libs/libgtest_main.a
注意:如果您收到一堆與 pthread 相關的鏈接器錯誤,則您忘記了第三個命令中的 -pthread。 如果你得到一堆與 C++ 運行時庫相關的鏈接器錯誤,你輸入的是 gcc 而不是 g++。
正如參考我有一個docker
系統設置與g++
和gtest
其正常工作。 我在這里提供所有文件以供將來參考:
FROM gcc:9.2.0
WORKDIR /usr/src/app
RUN apt-get -qq update \
&& apt-get -qq install --no-install-recommends cmake \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN git clone --depth=1 -b master https://github.com/google/googletest.git
RUN mkdir googletest/build
WORKDIR /usr/src/app/googletest/build
RUN cmake .. \
&& make \
&& make install \
&& rm -rf /usr/src/app/googletest
WORKDIR /usr/src/app
COPY . .
RUN mkdir obj
RUN make
CMD [ "./main" ]
CXX = g++
CXXFLAGS = -std=c++17 -Wall -I h -I /usr/local/include/gtest/ -c
LXXFLAGS = -std=c++17 -I h -pthread
OBJECTS = ./obj/program.o ./obj/main.o ./obj/program_unittest.o
GTEST = /usr/local/lib/libgtest.a
TARGET = main
$(TARGET): $(OBJECTS)
$(CXX) $(LXXFLAGS) -o $(TARGET) $(OBJECTS) $(GTEST)
./obj/program.o: ./cpp/program.cpp
$(CXX) $(CXXFLAGS) ./cpp/program.cpp -o ./obj/program.o
./obj/program_unittest.o: ./cpp/program_unittest.cpp
$(CXX) $(CXXFLAGS) ./cpp/program_unittest.cpp -o ./obj/program_unittest.o
./obj/main.o: ./cpp/main.cpp
$(CXX) $(CXXFLAGS) ./cpp/main.cpp -o ./obj/main.o
clean:
rm -fv $(TARGET) $(OBJECTS)
#include <iostream>
#include "program.h"
#include "gtest/gtest.h"
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
std::cout << "RUNNING TESTS ..." << std::endl;
int ret{RUN_ALL_TESTS()};
if (!ret)
std::cout << "<<<SUCCESS>>>" << std::endl;
else
std::cout << "FAILED" << std::endl;
return 0;
}
#include "program.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 if and only if 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;
}
#include <limits.h>
#include "program.h"
#include "gtest/gtest.h"
namespace
{
// Tests Factorial().
// Tests factorial of negative numbers.
TEST(FactorialTest, Negative)
{
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
}
// Tests factorial of 0.
TEST(FactorialTest, Zero)
{
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
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()
// Tests negative input.
TEST(IsPrimeTest, Negative)
{
// This test belongs to the IsPrimeTest test case.
EXPECT_FALSE(IsPrime(-1));
EXPECT_FALSE(IsPrime(-2));
EXPECT_FALSE(IsPrime(INT_MIN));
}
// Tests some trivial cases.
TEST(IsPrimeTest, Trivial)
{
EXPECT_FALSE(IsPrime(0));
EXPECT_FALSE(IsPrime(1));
EXPECT_TRUE(IsPrime(2));
EXPECT_TRUE(IsPrime(3));
}
// Tests positive input.
TEST(IsPrimeTest, Positive)
{
EXPECT_FALSE(IsPrime(4));
EXPECT_TRUE(IsPrime(5));
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
#ifndef GTEST_PROGRAM_H_
#define GTEST_PROGRAM_H_
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n);
// Returns true if and only if n is a prime number.
bool IsPrime(int n);
#endif // GTEST_PROGRAM_H_
cpp/program.cpp
和h/program.h
文件來自googletest
repo sample 1 。 Dockerfile
改編自此處。
我使用sudo apt-get install libgtest-dev
在我的系統上安裝了 Google Test 並且我正在使用的 Fixture 沒有main()
並且可以使用以下方法構建:
g++ unitTest.cpp -o unitTest /usr/src/gtest/src/gtest_main.cc /usr/src/gtest/src/gtest-all.cc -I /usr/include -I /usr/src/gtest -L /usr/local/lib -lpthread
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.