GoogleTest C++ - Test Fixture

2019-05-25 19:18发布

I'm a beginner in C++ and am coding a Snake for uni, and have to run unit tests. FYI, I code on Xcode 7.1.1.

I manage to make sample tests run on my machine, but have a problem when it comes to creating a fixture for my snake. Here is the code I have :

#include "gtest/gtest.h"
#include "calc.h"
#include "Serpent.h"
#include "Map.h"
#include "Main.h"
using namespace std;
using namespace sf;

class Serpent_test_fixture: public ::testing::Test
{public:
Serpent* serpent_test;
Map* map_test;

Serpent_test_fixture(){
    serpent_test = new Serpent(true);
    RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "SnakeTest", Style::None);
    map_test = new Map("", window, false);
}

virtual void SetUp(){
    map_test->updateGameField(0, 0, HEAD_EAST);
    serpent_test->setHead(*map_test, false);
    int size_init = serpent_test->getSize();
}


virtual void TearDown(){

}

~Serpent_test_fixture(){
    delete serpent_test;
    delete map_test;
}  
};
TEST_F(Serpent_test_fixture, cherry_action)
{
map_test->updateGameField(0,0,CHERRY);
Tiles head_tile_test = HEAD_EAST;
serpent_test->fruit_action(*map_test, head_tile_test);
EXPECT_EQ(20, 20);
}
int main(int argc, char * argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

The way I understand it is, I create my snake (serpent in French) from my class Serpent, then I create my map from my class Map.

UpdateGameField updates the tile in (0,0) of the map and puts "HEAD_EAST" on it.

Anyway, here is the message I have :

Undefined symbols for architecture x86_64:
  "Map::updateGameField(int, int, Tiles)", referenced from:
      Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o
      Serpent_test_fixture::SetUp() in main.o
  "Map::Map(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, sf::RenderWindow const&, bool)", referenced from:
  Serpent_test_fixture::Serpent_test_fixture() in main.o
  "Map::~Map()", referenced from:
  Serpent_test_fixture::SetUp() in main.o
  Serpent_test_fixture::~Serpent_test_fixture() in main.o
  "Serpent::fruit_action(Map&, Tiles&)", referenced from:
  Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o
  "Serpent::getSize()", referenced from:
  Serpent_test_fixture::SetUp() in main.o
  "Serpent::setHead(Map, bool)", referenced from:
  Serpent_test_fixture::SetUp() in main.o
  "Serpent::Serpent(bool)", referenced from:
  Serpent_test_fixture::Serpent_test_fixture() in main.o
  "Serpent::~Serpent()", referenced from:
  Serpent_test_fixture::~Serpent_test_fixture() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can anyone help? It's my first time using Google tests and I'm having a hard time making it work.

1条回答
在下西门庆
2楼-- · 2019-05-25 19:50

The basic issue here is how to orchestrate the dependencies for a test project relative to the production code to be tested and the test framework. The dependency relationships drive the build system (compiler, linker, etc.).

You might want to take a look at my C++ Now! 2014 presentation on Test-Driven Development in C++. It uses Boost.Test instead of google test, but shows a CMake based recipe to orchestrate the dependencies between production code and test code. The workshop is designed for you to follow along at your computer, replicating the steps in the presentation -- I provide the code you use at each step.

The dependencies look like this for a typical testing project:

  • test executable
    • production code to be tested (static library or shared library)
    • test framework

In my workshop materials I show how to use CMake to create these dependencies using Boost.Test as the test framework, but the principle is the same with google test and the recipe is nearly identical as well.

查看更多
登录 后发表回答