Monday, December 30, 2013

Using Test Fixture in Google Test #7

In this Blog you will learn how to optimize setting up common environments for your tests using test fixtures in google test. 

What is a test Fixture

A Test Fixture allows you to creates setup and teardown method to run before each test is run in your test case. Test Fixtures are easy to create in google test. You just need to create a class that is the same name as your test case and inherit from the testing::Test class.
class BitcoinWallet_ULT : public testing::Test { public:  virtual void SetUp() {  }  virtual void TearDown() {  }};

How to use the Test Fixture

Now that you have a test fixture you need to let you tests know how to use the fixture. This is easy as well. Use the TEST_F macro instead of the TEST macro.
  • Without Test Fixture
    • TEST(BitcoinWallet_ULT,updateLocalBalanceTest)
  • With Test Fixture
    • TEST_F(BitcoinWallet_ULT,updateLocalBalanceTest)
Only use the TEST_F macro for tests in you test case that you want to use the test fixture. You don't have to use TEST_F if you test does not require setup.

What happens when TEST_F is called

The TEST_F macro will connect your test into the test fixture. This means that the following things will happen each time the TEST_F macro is run.
  1. Instantiate a fixture class - Calls new on the BitcoinWallet_ULT class
  2. Call SetUp on the fixture 
  3. Call the test
  4. Call TearDown on the fixture
  5. Destroy the fixture - Calls delete on the BitcoinWallet_ULT object created in step 1

What goes in Setup

Anything that is required to run the test (environment) that is common between all of a majority of the tests. Many tests in the same suite will have common variables that they use to run the tests. This typically include Mocks, and setup environment variables. I typically put these variables in the fixture as attributes. Then in the Setup Method I instantiate the objects and assign them to the attributes so they can be used in the tests. Also don't forget to destroy any objects you create in SetUp in the TearDown method. 
class BitcoinWallet_ULT : public testing::Test { public:  string myAccount;  BitcoinWallet* wallet;  BitcoinBank_Mock* bankMock;  BitcoinCommerce_Mock* commerceMock;  BitcoinCurrency_Mock* currencyMock;  virtual void SetUp() {   ExternalService::theBank = bankMock = new BitcoinBank_Mock();   ExternalService::theCommerce = commerceMock = new BitcoinCommerce_Mock();   ExternalService::theCurrency = currencyMock = new BitcoinCurrency_Mock();   wallet = new BitcoinWallet();  }  virtual void TearDown() {   delete(wallet);   delete(bankMock);   delete(commerceMock);   delete(currencyMock);  }};
Notice in the SetUp that I am setting variables for the ExternalService as well as the attribute to be used in the TEST_F macro. This allows me to change the behavior of the mock in the test itself. Also for each object I create in the SetUp I have a corresponding delete in the TearDown.

Using the TEST_F Macro

Now that the fixture is complete setup I can use it in my test case. All I need to do is change the TEST macro to TEST_F and remove any of the environment setup that the fixture now handles for me.

Before TEST_F

TEST(BitcoinWallet_ULT,updateLocalBalanceTestNoAccount) {  BitcoinBank_Mock* bankMock; ExternalService::theBank = bankMock = new BitcoinBank_Mock();   ON_CALL(*bankMock, changeBalance(_,_))    .WillByDefault(Return(-1));   BitcoinWallet* wallet = new BitcoinWallet();   ASSERT_EQ(wallet->updateLocalBalance(),ActionStatus::AccountNotExist); }

With TEST_F

TEST_F(BitcoinWallet_ULT,updateLocalBalanceTestNoAccount) {  ON_CALL(*bankMock, changeBalance(_,_))   .WillByDefault(Return(-1));  ASSERT_EQ(wallet->updateLocalBalance(),ActionStatus::AccountNotExist); }

I hope this helps with making your Unit Level Testing more effective.

DWP

Monday, December 2, 2013

VS2012 and Gmock - Using ON_CALL and EXPECT_CALL Macros - Video #6

This is the sixth video in a series on how to use GoogleTest and GoogleMock with the VS2012 framework. Before watching this video you should make sure that you have already setup your Visual Studio solution to have a googlemock, googletest and a unit level test project. If you do not know how to do this check out the previous videos/blogs in the series. Here at "Using VS2012 and Google Test Video #1" in this blog.
In this video I will show you how to use the ON_CALL and EXPECT_CALL macros to set up default behavior for a mock object and to set expectations on method calls to the mocked object.


ON_CALL macro

This macro helps you set the behavior of the call to the method for the specified object. This is the default behavior of the object when the method is called with the specified arguments (matchers). The macros can be called as follows.
ON_CALL(mock_object, method(matchers))
    .With(multi_argument_matcher)  ?
    .WillByDefault(action);

The mactchers allows you to specify the values of the arguments that are passed to the method. This gives you the flexibility to set behavior based on parameters passed to the method. Very powerful when you are writing white box testing. The .With allows you to specify multiple matchers for the method call. The WillByDefault method allows you to specify the default action to perform when the method is called. Most of the time that is "Return".

Matchers

Matchers let developers set the default behavior based on the parameters passed into the method specified in the macro. This gives you the ability to change the behavior of the mocked object based on the parameters passed into the method. Googlemock gives several matching macros:
  • Wild Cards - 
    • “_” - Match anything
    • “A<type>()” – Match any type “int”, “char”, “Class”
  • Comparators
    • Eq(value), Ne(value), IsNull(), NoNull(), Ge(), Le(), …
  • String Matchers
    • StrEq(value), StrNe(value), ContainsRegex(string), …
Here is an example of using the ON_CALL macro to set the behavior of the method changeBalance when the first parameter is "MyAccount".
ON_CALL(*bbm, changeBalance(StrEq("MyAccount"),_))
  .WillByDefault(Return(100));
In this example any call to changeBalance with the first parameter equal to "MyAcount" will return 100. Pretty simple example, but you can see the power of being able to set behavior of a mock method in the test that is using the mock method. (High Cohesion, Low Coupling) :)

EXPECT_CALL macro

This macro is used to set the expectation of a mock method for a specific mock object. This allows you to write pinpoint white box testing for the class without having to write complex test frameworks for you specific architecture.
EXPECT_CALL(mock_object, method(matchers))
   .Times(cardinality)
   .WillOnce(action)
   .WillRepeatedly(action);

The Times, WillOnce and WillRepeatedly calls are options and can be used more than once.

Times

How many times will the method be called. If the number does not match then an assertion is set and the test will fail.

WillOnce

WillOnce specifies what we are expecting the function should do. The action that will take place. The order of WillOnce specifies the order of the expected actions. You can chain WillOnce calls together to show that a method is called several times with different behaviors. You can finish your change with WillRepeatedly to specify that the method can be called more times with specific action.

using ::testing::Return;

EXPECT_CALL(turtle, GetY())
   .WillOnce(Return(100))
   .WillOnce(Return(200))
   .WillRepeatedly(Return(300));

There are several good resources availble on the google mock site:
I hope this helps you understand googlemock a bit more and helps you move to a more Test Driven Development model. Happy developing.

DWP