2

I am trying to follow this tutorial for running a Google Tests file but I am having some trouble with the CMakeLists.txt.

https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6) # Locate GTest find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests tests.cpp) target_link_libraries(runTests ${GTEST_LIBRARIES} pthread) 

what exactly is runTests here? Is it my program's main() file? How should I replace it according to my program? Currently, I have this error when I use make:

/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status CMakeFiles/runTests.dir/build.make:95: recipe for target 'runTests' failed make[2]: *** [runTests] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed make[1]: *** [CMakeFiles/runTests.dir/all] Error 2 Makefile:83: recipe for target 'all 

main.cpp: (I tried moving this part to the end of the tests.cpp file too but still didn't work)

#include <iostream> #include "Player.h" #include "gtest/gtest.h" using namespace std; int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 

Player.h

#ifndef PLAYER_H #define PLAYER_H #include <iostream> using namespace std; class Player { int inventory; public: Player(); int decreaseInventory(int numOfBeers); void setInventory(int newInventory); int getBackOrder(); int getCost(); int getInventory(); bool operator ==(Player& p); }; Player::Player() { cout << " Default Player Constructor\n"; inventory = 12; backorder = 0; cost = 0; orderDelay = 0; shipmentDeplay = 0; } void Player::setInventory(int newInventory) { inventory = newInventory; } int Player::decreaseInventory(int numOfBeers) { inventory = inventory - numOfBeers; } int Player::getInventory() { return inventory; } #endif 

tests.cpp

#include "gtest/gtest.h" #include "Player.h" TEST(playerTest, decreaseInventoryTest ) { Player p; int curr_inv = p.getInventory(); EXPECT_EQ(curr_inv-3, p.decreaseInventory(3)); } 

How can I run my tests?

1
  • FWIW, the tutorial is a bit dated, particularly as it pertains to the CMake file. The use of include_directories() is discouraged, as it has been superseded. Also, the imported GTest::GTest target should be preferred when using the FindGTest module. Commented Mar 11, 2020 at 15:26

2 Answers 2

1

what exactly is runTests here? Is it my program's main() file? How should I replace it according to my program?

runTests is the name of an executable that you're trying to build. It needs a main just like any executable will, and there are two options: 1) write your own or 2) use the one provided with gtest. If you want to go with option 1, add main.cpp to the add_executable line.

add_executable(runTests tests.cpp main.cpp) 

The better option in my opinion is to use the main provided by GTest, since it gives you some command line arguments. You can use that by adding it to your target_link_libraries line.

target_link_libraries(runTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread) 
Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to recompile my main.cpp or something else if I change my code? Currently if I run ./runTests, it keeps saying that the test has failed. Tho everything else looks correct to me. Expected: curr_inv-3 Which is: 9 To be equal to: p.decreaseInventory(3) Which is: 1862945232
make should rebuild all targets that depend on your changed files. 1862945232 looks like something's either been uninitialized or underflowed, so I'd suggest using gdb to figure out what's going on.
Fixed it! I had forgotten to return values from decreaseInventory();
0

Check the official guide https://google.github.io/googletest/quickstart-cmake.html

FetchContent_Declare(...) target_link_libraries( runTests gtest_main ) 

Comments