Unit testing series – Test without double (Part 3)

The first test szenario isn’t using a double.


Test

#pragma once
#include "WindowManager.hpp"
#include "UserInterface.hpp"
#include <gtest/gtest.h>
#include <memory>

class UserInterfaceTest : public testing::Test
{
protected:
  virtual void SetUp();
  virtual void TearDown();
  std::shared_ptr<IWindowManager> m_windowMgr;
  std::unique_ptr<UserInterface> m_userInter;
};
#include "UserInterfaceTest.hpp"

void UserInterfaceTest::SetUp()
{
  m_windowMgr.reset(new WindowManager(100, 100));
  m_userInter.reset(new UserInterface(m_windowMgr));
}

void UserInterfaceTest::TearDown() {}

TEST_F(UserInterfaceTest, loadWindow)
{
  bool done = m_userInter->loadWindow(50, 150, 50, 150);
  EXPECT_NE(done, true);
}

App

#include <gtest/gtest.h>

int main(int argc, char **argv)
{
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Exec

$ g++ -c UserInterface.cpp WindowManager.cpp
$ ar rcs libwnd.a UserInterface.o WindowManager.o
$ g++ UserInterfaceTest.cpp Main.Test.cpp libwnd.a -lpthread -lgtest
$ ./a.out 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from UserInterfaceTest
[ RUN      ] UserInterfaceTest.loadWindow
[       OK ] UserInterfaceTest.loadWindow (0 ms)
[----------] 1 test from UserInterfaceTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.