Saturday, August 14, 2010

Visual C++ and Googletest

This post will probably only be of interest to Chip, but I'm writing it anyway.

  • First step was to download my software packages. I got visual C++ 2010 from http://www.microsoft.com/express/Windows/. Then I got the version 1.5 zip of Googletest from http://code.google.com/p/googletest/downloads/list.
  • Visual C++ installed without a hitch, although there is a mandatory reboot required by the install. Googletest doesn't even require an install, you just unzip the package and stick it somewhere.
  • Looking for a pattern to follow, I found http://www.freshclickmedia.com/blog/2009/10/getting-started-with-googletest-the-google-c-testing-framework/. This is a short video, and being a video is both it's greatest strength and weakness. If you use it, you'll probably be rewinding a lot and cursing the inability to cut and paste. However, watching someone clicking on the interface is very useful. This really covers everything you need to get up and running, but you have to play close attention. I cost myself some time searching for files that were pointed out in the video by not rewinding far enough.
  • The fist step is to compile the vcpp version of Googletest. The google example was probably built as a vcpp 2008 project because my version told me I needed to convert the project. At first the conversion failed because the project was read-only. I figured that I had the original zip, so it wouldn't hurt to make these files read/write. After I did this the project built without any issues.
  • There are two "gotchas" I missed the first time through. The first is the runtime library. Multi-threaded debug and multi-threaded debug dll are not the same thing. There is a warning about linker errors if you get these wrong, but the warning in the video isn't really stern enough. The other is that the 2 libraries built when you compile Googletest ~\msvc\gtest\Debug where ~ is wherever you unzipped Googletest. This is the part where I didn't rewind far enough and had to hunt for these files.
  • I started a new console project. You need to add the two libraries, gtest_maind and gtestd to Configuration Properties | Linker | Input.
  • Next, under Configuration Properties | c/c++ | General, add the ~\googletest\include to Addition Include Directories.
  • Time for the second issue I ran into, I didn't set Configuration Properties | c/c++ | Code Generation to Multi-threaded Debug (/MTd). The default is Multi-threaded Debug DLL (/MDd) which is very similar looking, but not quite the same.
  • I've seen a few ways to include the tests. The one that seems the most intuitive to me is to set up a header file which declares a test class and has the constructor, destructor, SetUp, and TearDown methods. This creates a class which can be used as a test suite. The easiest way to get started is to paste in some boiler plate code from http://code.google.com/p/googletest/wiki/GoogleTestPrimer.
  • The actual tests go into a cpp file with the same name as the header. Here is a simple test set up by the TEST_F Macro: TEST_F (SimpleTest,DemoTest1) { ASSERT_EQ(1,1); }
  • Two lines of code go into the main function to cause the tests to run:
  • ::testing::InitGoogleTest(&argc, argv);
  • return RUN_ALL_TESTS();
  • One thing I like about this is that you just tell it to run all tests, you don't have to list each test suite you created. For my simple, just get it running, project, this doesn't matter, but for a large project, automatic inclusion seems like a very nice feature.
  • My first try was a complete loss because of the run time library mistake I made. My second try, I realized my mistake and I got to see the test results pop up at the end of my run.

No comments: