Tuesday, October 29, 2013

VS2012 & Google Test - Running Unit Level Tests - Video #3

This is the next in a series of videos on how to use VS2012 and Google Test. The typical "Ctlr-F5" in VS2012 will bring up and run the unit level tests for the project. But that can be hard to filter through all of the tests that are run. Especially when you are running hundreds or thousands of tests in your project.
This video will show you how to run unit level tests in the VS2012 using a gtest addin. It will show also how to run unit level tests from the command line. 

The video covers the following items.
  1. Adding the Google Test Add-in
  2. Run Tests(All, Selected, Failed)
  3. Debugging Tests
  4. Running from the command line
  5. Integrating into CI



First we need to download and install the visual studio google test add-in. It can be found at http://visualstudiogallery.msdn.microsoft.com/f00c0f72-ac71-4c80-bf8b-6fe381548031/file/87766/3/GoogleTestAdapter.vsix.
Once you have install this you will need to restart visual studio.

Now you will see a new menu item TEST. Select TEST and you can see many options for you, including Run All, Run Selected, and Run Failed. This plug in makes it much easier to work with your tests and your code. You can also debug your tests with this plugin. Just select the Debug Test options in the TEST menu.

Integrating your Unit Level Tests into you build or continuous integration system requires running the tests from the command line. Google Test has that ability as well. But you will need to write your own main program. First create a main.cpp file and put the following code in the file:
int main(int argc, char** argv) {
 
// This allows the user to override the flag on the command line.
  ::testing::
InitGoogleTest(&argc, argv);

  return RUN_ALL_TESTS();
}
It is important to include the ::testing::InitGoogleTest call as it sets up the options for running the tests. This allows us to pass command line arguments to the executable.Some of the most common arguments are:

  • --gtest_filter=* - Runs all the tests
  • --gtest_filter=BitcoinTransaction.* - Only run the BitcoinTransaction tests
  • --gtest_repeat=1000 – Repeat tests 1000 times
  • --gtest_repeat=-1 – Run forever
  • -- gtest_output=xml – Great for CI that can parse junit
  • --help  - list all of the options for running test commands.
Hope this helps with the velocity and quality of your product development.  Check out my next videos that will discuss architecting tests and using test mocks.

DWP




Monday, October 21, 2013

VS2012 and Google Test - Using TEST, ASSERT, and EXPECT (Video #2)

This video shows the basic steps to writing Tests in the googletest framework. This is a followup video to Getting Started. When running Unit Level Tests you want to be able to quickly find the tests that failed and get to the code and conditions that caused the failure. One of the tricks you can use is to come up with a naming standard and to use them.

Here is a naming standard that I use:

  • Test Filename - Name of the class or file testing followed by _ULT
  • Test Case Name - Name of the class or filename that you are going to test
  • Test Name - Name of the method followed by the condition you are testing.
    • methodName-PositiveCondition
    • methodName-NegativeCondition
Using these naming standards in googletest is easy using their TEST Macros.
TEST(testCaseName, testName) {
...
}
Where testCaseName is the name of the Test Case and testName is the name of the test. Here is an example of these standards in action.

BitCoinTransaction_ULT.cpp

TEST(bitCoinTransactionULT,addMoney-PositiveAmount) {
...
};

TEST(bitCoinTransactionULT,addMoney-NegativeAmount) {
...
};

In this case we added two tests both in the bitCoinTransactionULT Test Case. Both tests are testing the method addMoney. The conditions PostiveAmount and NegativeAmount are being tested. It is ok to be descriptive. It will make it much easier to find out what is going in when you are looking through test results of hundreds if not thousands of tests.

Here is a quick overview of ASSERT and EXPECT.

—ASSERT_*

—If an assert fails it causes a fatal failure and returns from the current function.
—ASSERT_TRUE(condition) - True
—ASSERT_FALSE(condition) - False
—ASSERT_EQ(expected, actual) - Equal
—ASSERT_NE(expected, actual) - Not Equal
—ASSERT_LT(val1,val2) - Less Than,
ASSERT_LE(val1,val2) - Less Than Equal To,
ASSERT_GT(val1,val2) - Greater Than,
ASSERT_GE(val1,val2) - Greater Than Equal To

—EXPECT_*

—If an expect fails it does not cause a fatal failure. It just flags the failure in the logs
—EXPECT_TRUE(condition) - True
—EXPECT_FALSE(condition) - False
—EXPECT_EQ(expected, actual) - Equal
—EXPECT_NE(expected, actual) - Not Equal
—EXPECT_LT(val1,val2) - Less Than,
EXPECT_LE(val1,val2) - Less Than Equal To,
EXPECT_GT(val1,val2) - Greater Than Equal,
EXPECT_GE(val1,val2) - Greater Than Equal To

Monday, October 14, 2013

Using VS2012 and Google Test - Getting Started #1

I recently helped teach a class on Unit Level Testing using GoogleTest and VS2012. It was not a trivial as I thought it would be. But once I figured out some tricks to get everything running it works great. This blog and video is the first in a series of posts that will show how to effectively perform Unit level testing using GoogleTest and VS2012.

Setup GoogleTest in VS2012

  • Download googletest https://code.google.com/p/googletest/downloads , and extract to a directory:
  • Create a new project.
    • Right-mouse click on the Solution, and Add->New Project
    • Name the project googletest
    • Create a new project of static library: Add->New Project->Win32 Project->Static Library; Uncheck “precompiled header”
  • Compile googletest into a library.
    • Add include path , /include
    • Add source file /src/gtest-all.cc and gtest_main.cc
    • Change the pre-processor rules to include the definition. Project->Properties->C/C++->Preprocessor->Definitions Add _VARIADIC_MAX=10 (warning! Make sure not to have spaces in this definition)
    • Build the Project (F7)
  • Add a unit level test Project
    • Right-mouse click on the Solutuion, and Add->New Project
    • Create a new “Win32 Project-> Console application” project; Call it BitCoinTest; Uncheck “precompile header”.
    • Add include path and References…->Add Reference check both Bitcoin and googletest
    • Change the pre-processor rules to include the definition _VARIADIC_MAX=10.
    • Right-click on the project, and “Set as StartUp Project”
  • Build the solution
  • Run the default Project (Ctl-F5)
Look for the next blog GoogleTest and VS2012 - Writing you first Test Cases.