Sunday, March 13, 2016

Starting Test Driven Development [TDD]

For a serious developer, Test drive development is a must. It will save you from many difficult situations.
In this blog, we will see how to use Google Test framework for c++.

There could be two ways to include google test framework in your project.

  • Download google test framework from GitHub, Build it and use it
  • Use NuGet Package Manger in visual studio to install and use it

Starting with google test. A simple example

#include <iostream>
#include <gtest\gtest.h> // This file is required for google test


// Test Fixuture class
class TestAirthmatic:public testing::Test
{
public:
virtual void SetUp()override {}
virtual void TearDown()override {}
int add(int aint b) { return a + b; }
int divide(const int valconst int by)
{
return val / by;
}
};

// Writing test case
TEST_F(TestAirthmatic, Demo_passCase) { EXPECT_EQ(4, add(2, 2)); }// A demo test which will pass
TEST_F(TestAirthmatic, Demo_FailCase) { EXPECT_EQ(4, add(3, 2)); }// A demo test which will fail
TEST_F(TestAirthmatic, DISABLED_test) {} // A disabled test case

int main(int argcchar** argv)
{
testing::InitGoogleTest(&argcargv);
return RUN_ALL_TESTS();
}

Output of Test is





No comments:

Post a Comment