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?