Testing

Google Test with Visual Studio (2010) – A Quick Start Guide

Google Test is the Google’s framework for writing C++ tests on variety of platforms. For more information you can go to Google Test project home. Google Test is not the only C++ testing framework. You can find full list of C++ testing frameworks here.

However setting up Google Code and writing the first test case might be little challenging for a first time user. This blog post will walk you through setting up and running your first test case using Google Test.

1. Downloading and build Google Test

Go to the Google Test home page : https://code.google.com/p/googletest/ and select the second tab which is “Downloads”. Then you will be presented the available versions of Google Test. Select one and download.GoogleTestDownload

Once downloaded and extracted, you will find a “msvc” folder inside. In there you will find “gtest” visual studio solution file. Open that in Visual Studio. You will find  the following 4 projects.

gtestProjectStructureClean and build gtest and gtest_main projects.  For this scenario only a debug build is sufficient.

2. Create a project and write a program which you need to test.

Once you have built the gtest project , create a new project in Visual Studio. For demonstration purposes I made a simple calculator application. Calculator.h and Calculator.cpp is as follows.

Calculator.h

class Calculator
{
public:
int add(int a, int b);
};

Calculator.cpp

#include "Calculator.h"

int Calculator::add(int a, int b)
{
return a+b;
}

Now starts the sticky part. Before getting your hands dirty I highly suggest you go through this article in Google Test documentation. If you are just lazy, never mind . Just continue with following steps.

At this point you have a method to test (add method in calculator class). Now you have to create the main class. There is a boilerplate main class provided by Google Test documentation. But since we are not going in depth in this stage following is the edited main class which will suit for our requirement.


#include "Calculator.h"
#include "gtest/gtest.h"

namespace {

class CalculatorTest : public ::testing::Test {

};

// Test Calculator::add method
TEST_F(CalculatorTest, CalculatorAddMethod) {
Calculator cal;
EXPECT_EQ(10, cal.add(6,4));

}

}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
getchar();
return 0;
}

3. Project Settings Configuration

Now you have to configure the following settings. Right click the project and select Properties.

3. 1 Additional include directories in C++

Configuration Properties -> C/C++ -> General -> Additional Include Directories -> Edit

add path of the include directory in the gtest folder you downloaded.

ConfigurationProperties13.2  Additional Library Directories in Linker

Configuration Properties -> Linker -> Additional Library Directories -> Edit -> Select the debug directory of the msvc folder in gtest downloaded folder.

ConfigurationProperties23.3 Additional Dependencies in Linker

Configuration Properties -> Linker -> Input ->Additional Dependencies -> Edit -> Add gtestd.lib and gtest_maind.lib.

ConfigurationProperties33.4  Runtime Library in C/C++

Configuration Properties -> C/C++ -> Code Generation ->Runtime Library -> Select “Multi-threaded Debug (/MTd)”

ConfigurationProperties44. Build and run

Now if you build and run the test, it should give you the following output.

PassingTestThis is how it will look like when all the tests are passed.

Now change the assertion line in main.cpp to following.

EXPECT_EQ(10, cal.add(6,6));

In this case our assertion is wrong, hence the test should fail. Now build and run this and you will get the following output.
FailingTest

Advertisements

Why do it in the hard way : A guide to JUnit Parameterized Tests

If you have a set of parameters that your method should be tested, here is an easy way. For an example, let’s take that you have a method to find a number is even or not. You want to test that for some random set of given numbers, whether the method works fine.

The method in Java to find even numbers looks like this:


public class EvenNumberChecker {
public boolean checkForEvenNumber(int number){
boolean isEven=false;
if(number%2==0){
System.out.println("Number "+number+" is Even");
isEven=true;
}else{
System.out.println("Number "+number+" is Odd");
isEven=false;
}
return isEven;
}
}

Then here is where the magic comes in. You have to create a JUnit test case and annotate it with @RunWith(Parameterized.class). Then you have to create the constructor using the parameters to be tested as an arguments. Then annotate a method with @Parameterized.Parameters and add the parameters to be tested through this method. You have to be careful that this method should return an iterable list. Here is what the whole test case looks like.


@RunWith(Parameterized.class)
public class TestEvenNumberChecker {
private EvenNumberChecker evenNumberChecker;
private Integer inputNumber;
private boolean isEven;

@Before
public void initialize() {
evenNumberChecker = new EvenNumberChecker();
}

// Parameter to be tested should be an argument here.
// In this case we have two parameters, that is the input number and the boolean value of indicating whether the number is even.
public TestEvenNumberChecker(Integer inputNumber,boolean isEven) {
this.isEven=isEven;
this.inputNumber = inputNumber;
}

// We have to add the parameters to be tested through this method
@Parameterized.Parameters
public static Collection Numbers() {
return Arrays.asList(new Object[][]{{2314,true},{456834,true},{678423,false},{4632489,false}});
}

@Test
public void testPrimeNumberChecker() {
System.out.println(“Checking Number : “+inputNumber);
assertEquals(isEven, evenNumberChecker.checkForEvenNumber(inputNumber));
}
}

You can download the full implementation at https://github.com/sandarumk/ParameterizedTestTutorial