Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction Introduction

Introduction

Top  Next

tabssoftbox

Start unit testing in C++ with Isolator++!

 

Isolator++ enables easy unit testing of existing and new C++ code.

 

Unit testing in C++ is a hassle. But once you've decided to do it, you're faced with the main problem of unit testing in any language - you can't unit test the code, because it depends on classes and external resources. Without tests, the code is vulnerable to regression bugs, and therefore you usually don't touch it, and are scared just from the thought.

 

Isolator++ allows you change the behavior of dependencies in your code, without changing it. You build a test application using the framework of your choice (GoogleTest, UnitTest++, CppUnit, etc.), include Isolator++ header file, link with the Isolator++ library and then use the Isolator++ APIs in your tests to change the behavior of method calls to dependencies. All this without changing their code. Isolator++ contains innovative features to keep your tests short, readable and robust.

 

Here's a short example. Let's look at our Product class' method IsExpired:

 

bool Product::IsExpired()

{

   time_t rawtime;

   struct tm * now;

 

   time(&rawtime);

   now = localtime(&rawtime);

  

   if (now->tm_year > 2017)

   return true;

   return false;

}

 

The method has a dependency on the time when the code is run. How can we test the result for a year after 2017?

With Isolator++'s macros (in red), you can simply change the behavior of the GetSystemTime method:

 

TEST_F(IsolatorPPTests, IsExpired_YearIs2018_ReturnTrue)

{

 Product product;

 

 // Prepare a future time construct

 tm* fakeTime = new tm();

 fakeTime->tm_year = 2018;

 

 // Fake the localtime method

 FAKE_GLOBAL(localtime);

 

 // Replace the returned value when the method is called 

 // with the fake value.

 WHEN_CALLED(localtime(_)).Return(fakeTime);

 

 ASSERT_TRUE(product.IsExpired());

}

 

 

When the IsExpired method is called, the call to GetSystemTime is ignored, and then returns the value we supply - our future time construct.

Without changing the code, we actually we able to simulate the GetSystemTime to check a second path in the code.

 

To learn more about how to use Isolator++, check out the next chapters:

 

 


Copyright © Typemock Ltd. 2009-2018.  All Rights Reserved.