# How to properly setup unit testing for my c++ game project? [closed]

I'm currently creating a 2d game in C++ using visual studio and utilizing gtest/gmock for my testing framework. The problem I'm having is I'm not sure what my best option is for testing. I'm using SDL and opengl and if I try and test normally my test project crashes when it hits any opengl calls. I'm using glew to access opengl so in my game I setup glew and call glew.init() within my window class:

void Window::Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL Error", "SDL did not initialize", nullptr);
SDL_Quit();
}

/** All attributes need to be set before window creation **/

p_window = SDL_CreateWindow("Shadow Gods", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
if (!p_window)
{
LOG("ERROR: SDL window failed to initialize! SDL error: %s\n", SDL_GetError());
SDL_Quit();
}

glContext = SDL_GL_CreateContext(p_window);

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
LOG("ERROR: GLEW failed to initialize!");

SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(p_window);
SDL_Quit();
}

//Sets the values that the depth and color buffers will be set to when glClear is called
glClearDepth(1.0);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}


Now in order to test any of my classes that utilize openGL calls I need to make sure to create a window object first and initialize before calling any other code:

#include <gtest\gtest.h>
#include "GameEngine\Window.h"
#include "GameEngine\Sprite.h"

TEST(SpriteClassTest, SpriteInitialize_True)
{
Blz::Window win;
win.Initialize();
Sprite sprite;
ASSERT_TRUE(sprite.Init(23, 32, 100, 100, "../Game/CharImage.png"));
}


I don't want to have to do this for every class that utilizes opengl since 1.) It's redundant and 2.) My tests might be failing because there is something wrong with the window initialize() function instead of what tests were designed for. So is there a better way? If not, how could I get around having to include my "window.h" header in every file and having to initialize the window in every test? Can I do this globally with gtest?

## 1 Answer

This is mostly a comment, but unfortunately too long to post it as such.

Related: How can I unit test a GUI?

If your test requires you to create a Window, it's not a Unit Test. It might be a useful test, but it's not a Unit Test.

You test your classes by designing them such that if they logically don't require a Window, they don't rely on a Window. You also design classes such that very few of them logically require a Window.

To address your specific example, the example also seems to involve disk access, which is something some (but not all) people put in the same category as creating a Window. The condition of the test is also too little to be of actual use, and the naming of the test isn't at all descriptive, so I'll assume you're somewhat new to Unit Tests. I'll recommend to borrow some books on the topic from a local library, because web articles are too short to properly cover the topic, in my opinion.

• Please fix the link. I do not have enough reputation score. – zwcloud Sep 18 '18 at 5:41