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++ Professional!

 

Isolator++ Professional 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++ Professional allows you to change the behavior of dependencies in your code, without changing the code itself. You build a test application using the framework of your choice (GoogleTest, UnitTest++, CppUnit, etc.), include Isolator++ Professional header file, link with the Isolator++ Professional library and then use the Isolator++ Professional APIs in your tests to change the behavior of method calls to dependencies. All this without changing their code. Isolator++ Professional 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++ Professional's macros (in red), you can simply change the behavior of the localtime 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 localtime is ignored, and then returns the value we supply - our future time construct.

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

 

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

 

 


Copyright  Typemock Ltd. 2009-2023.  All Rights Reserved.