Category Archives: Win32

Test Driven Development (TDD) with Google C++ Testing Framework and Visual Studio (III)

And first of all, why should I use Google Test Framework?  There are several reasons to choose Google Framework between other options and you can find out more about it on their website, but I’ll give you two of my reasons:

  • It’s designed to be portable. You can use it on Windows, Mac and Linux and I can use it on Visual Studio and Borland Builder C++. That’s cool.
  • Automatically detects your tests and you don’t have to set specifically wich tests must run.

Ok, let’s start downloading the framework from here.

Unzip the gtest file and put it in the Visual Studio Projects folder or in any other folder where you have your projects. You’ll see several folders, each one belongs to a different platform. In our case we’re going to use the Visual Studio’s framework, so go inside the “msvc” folder, open the gtest solution and compile and then follow the steps:

  1. Create a new console test project with Visual Studio and C++ language.
  2. Add to this solution the projects “\msvc\gtest” and “\msvc\gtest_main” of the “gtest” solution that we compiled before.
  3. Go to the project Properties and in the Configuration Properties > C/C++ > Code Generation set “Basic Runtime Checks” to “Both (/RTC1, equiv. to /RTCsu) (/RTC1)” and set “Runtime Library” to “Multi-threaded (/MT)”.
  4. In this Properties page, set the “Optimization” option to “Disabled”.
  5. Also in  Configuration Properties > C/C++ >General > Additional Include Directories add this line “$(InputDir)\gtest-1.3.0\include” for All Configurations (Relese/Debug).

Ok, now add a new .cpp to our Test Project, for example “TestBattery1.cpp” where we are going to write our first Test using an example that you can find in the Google Framework website, which defines three steps:

  1. Include necessary header files.
  2. Use the TEST macro to define your tests.
  3. Call RUN_ALL_TESTS() in main().

In this code example, we test the function Factorial. You can find the function here and the tests code in this link:

// Tests factorial of 0.
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}

And that’s all for today.Finally, I leave here  a very good book about TDD but in this case with C#:

http://www.amazon.com/Professional-Test-Driven-Development-Applications/dp/047064320X/ref=sr_1_2?ie=UTF8&qid=1323605071&sr=8-2

Advertisements