top of page

First test - gtest and gmock part 4

Hi!

It is great day that I created next post which in not connected with basics – maybe in close future I will start write about shaders in Unity or openGl it is depend on you and your interesting. So we have almost last post about gTest and gMock – much time to learn. Let’s start it!

From the time when automatically testing became to be fancy in big companies with a lot of produced code – they lose money for not working services and to prevent code, applications, programs. They started to thinking “How to defense my code from errors/bugs/not working features?” choose one J and they discover few ways to test it.

One from the most obvious is test code by different code like we want to do it now. So we create a functions to test other functionality, this method of working try to eliminate many of testers - they are useless on this low level, because programmers make it. Of course we have system, integrational, smoke test, but low level is protected by static code analyze via experienced programmer and from functional point of view by unity tests. We try to make our programs better and better and unit tests prevent us before not working code and regression.

I need write post about testing – I use tones of maybe not understandable words :D I was tester and if you want I can talk few things about it if you want. Want you? If yes send me message or comment.

About tests you have two options:

  • EXPECT

  • ASSERT

Expect mean that you want to have known result, but if it will fail you go dipper and run next tests. At the end you have report – this test not pass and you can do it something with it.

Assert mean that the test is critical. So when assert test will fail, gTest will stop execution of tests which was not started. So mainly you want use assert just for core of your system and for features you should use expect.

Based on this you can make logical operations checking code.

Download: https://github.com/google/googletest

gTest_test.cc

----------------------------------

#define private public

#define protected public

#include <gtest/gtest.h>

#include "human.h"

#include "archer.h"

#include "warrior.h"

using namespace ::testing;

archer Maniek;

warrior Bronek;

TEST(humanTest, PositiveNos) {

EXPECT_EQ(0, human::getCounter());

EXPECT_NE(-1, human::getCounter());

EXPECT_NE(1, human::getCounter());

human::incrementCounter();

EXPECT_EQ(1, human::getCounter());

}

TEST(archerTest, PositiveNos) {

EXPECT_EQ(1, archer::getCounter());

EXPECT_NE(0, archer::getCounter());

EXPECT_NE(2, archer::getCounter());

EXPECT_EQ(1, Maniek.getCounter());

EXPECT_NE(0, Maniek.getCounter());

EXPECT_NE(2, Maniek.getCounter());

archer::incrementCounter();

EXPECT_EQ(2, archer::getCounter());

EXPECT_EQ(100, Bronek.getHealth());

Maniek.attack(&Bronek);

EXPECT_EQ(90, Bronek.getHealth());

EXPECT_EQ(0, Maniek.position);

Maniek.move(2);

EXPECT_EQ(2, Maniek.position);

}

TEST(warriorTest, PositiveNos) {

EXPECT_EQ(2, warrior::getCounter());

EXPECT_NE(1, warrior::getCounter());

EXPECT_NE(3, warrior::getCounter());

EXPECT_EQ(2, Bronek.getCounter());

EXPECT_NE(1, Bronek.getCounter());

EXPECT_NE(3, Bronek.getCounter());

warrior::incrementCounter();

EXPECT_EQ(3, warrior::getCounter());

EXPECT_EQ(100, Maniek.getHealth());

Bronek.attack(&Maniek);

EXPECT_EQ(90, Maniek.getHealth());

EXPECT_EQ(0, Bronek.position);

Bronek.move(2);

EXPECT_EQ(2, Bronek.position);

}

int main(int argc, char** argv) {

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();

}

Subscribe me!

Be sure that you read all.

  • Facebook Clean
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon
Recent Posts
bottom of page