GoogleTest C ++ - 测试夹具 [英] GoogleTest C++ - Test Fixture

查看:419
本文介绍了GoogleTest C ++ - 测试夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的初学者,我为uni编写了一个Snake,并且必须运行单元测试。
FYI,我在Xcode 7.1.1上编码。



我设法在我的机器上运行样本测试,但是在创建一个我的蛇的夹具。这里是我有的代码:

  #includegtest / gtest.h
#includecalc.h
#includeSerpent.h
#includeMap.h
#includeMain.h
using namespace std;
using namespace sf;

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

Serpent_test_fixture(){
serpent_test = new Serpent(true);
RenderWindow窗口(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();
}

我的理解是,我创造了我的蛇从我的类Serpent,然后我从我的类Map创建我的地图。



UpdateGameField ,0),并在其上放置HEAD_EAST。



无论如何,这里是我有的消息:

 架构x86_64的未定义符号:
Map :: updateGameField(int,int,Tiles),引用自:
Serpent_test_fixture_test_cherry_action_Test :: TestBody 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),引用自:
Serpent_test_fixture :: Serpent_test_fixture()in main.o
Map ::〜Map ),引用自:
Serpent_test_fixture :: SetUp()in main.o
Serpent_test_fixture ::〜Serpent_test_fixture()in main.o
Serpent :: fruit_action(Map& ),引用:
Serpent_test_fixture_test_cherry_action_Test :: TestBody()in main.o
Serpent :: getSize(),引用自:
Serpent_test_fixture :: SetUp
Serpent :: Serpent(bool),引用自:
Serpent_test_fixture :: SetUp()in main.o
Serpent :: Serpent
在main.o中的Serpent_test_fixture :: Serpent_test_fixture()
Serpent ::〜Serpent(),引用自:
Serpent_test_fixture ::〜Serpent_test_fixture()in main.o
ld:没有为架构x86_64找到符号
clang:error:linker命令失败,退出代码1(使用-v查看调用)

任何人都可以帮助?这是我第一次使用Google测试,我很难使其工作。

解决方案

这里的基本问题是以编排相对于要测试的生产代码和测试框架的测试项目的依赖性。依赖关系驱动构建系统(编译器,链接器等)。



你可能想看看我的C ++ Now! 2014演示文稿 C ++中的测试驱动开发。它使用Boost.Test而不是google测试,但显示了一个基于CMake的配方来协调生产代码和测试代码之间的依赖关系。该工作坊是为您在您的计算机上进行跟踪,复制演示文稿中的步骤 - 我提供您在每个步骤使用的代码。



依赖关系看起来像这是一个典型的测试项目:




  • 测试可执行文件


    • 要测试的代码(静态库或共享库)

    • 测试框架




在我的研讨会材料中,我展示了如何使用CMake创建这些依赖关系使用Boost.Test作为测试框架,但原则是相同的谷歌测试和配方几乎相同。


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.

解决方案

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.

这篇关于GoogleTest C ++ - 测试夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆