Category Archives: C++

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

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

Probably, like every developers, what you want is to start coding and see how is that of Google Testing Framework and how does it works. But not yet, my friend. Today we are going to have a look to the Unit Testing concept.

A “Unit Test” is a piece of code implemented to test a unit of work, which means that we are going to check the correct implementation of a requirement. For me the more important characteristics of a unit test are:

  • Repeatable and predictable.  It has to be designed to be executed  every time you needed and the result always must be consistent. Imagine that you’re testing an algorithm and you launch the test and it’s returns different results to the same parameters. Not good….
  • Isolated. You have to test only what is inside that function. Nothing else. That means that other modules, functionality or parts of your application shouldn’t be tested. For example, if you’re testing a function that reads a XML tag, don’t test in the same unit test a XML write routine.

Now, the process of executing unit testing is:

  1. Prepare the test performing some operation, initializing variables, etc…
  2. Launch the test.
  3. Check results
Ok, enough, it’s too much theory. We love coding and everything is much clear with examples, so in the next post we will start creating our first Unit Test with Google C++ Testing Framework!
Stay tuned!

 

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

Test Driven Development has become one of the most important and known paradigms in software development.  We could say that is part of the Agile methodologies that are changing the traditional Waterfall way of work.  But, what exactly TDD is?

If we summarize in a few words, we could say that TDD is just write first the test, and then write the code. That’s it.  At the beginning it might seem a bit strange and you have to change the way you think when you are about to write a routine or a new class, but it has a lot of benefits that worth it:

  • Ensures quality from the start. If you have any bug, the test will fail at first attempt.
  • Maintains clean your modules of unused code.
  • Provides regression testing. If you change a piece of code, you can be sure that it will not affect to any other functionality.
  • Produces simpler and more focused APIs, classes and libraries. When you are developing a method that has to pass a specific test, you avoid the temptation of mess up your method with a different functionality.
  • It’s much easier to match the code and business requirements.

There are a lot of different frameworks to implement Unit Testing and start to practice with TDD. Here you can find a huge list.

In this series of post for C++ language, I’m going to focus on the Google C++ Testing Framework. It’s been developed by Google and I find it quite simple and useful.  In the next post we will explain how to use it and in subsequent posts I’ll try to explain other frameworks for .NET.

Stay tuned!