Google C++ Testing Framework in Visual Studio 2008

  • 6,730 views
Uploaded on

How to set up the C++ xUnit framework created by Google in Visual Studio 2008.

How to set up the C++ xUnit framework created by Google in Visual Studio 2008.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
6,730
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
123
Comments
1
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Using Google Test with Visual Studio 2008 Andrea Francia (Galileian Plus) XPUG (14 aprile 2010) http:// www.galileianplus.it / http:// www.andreafrancia.it / http://blog.andreafrancia.it/
  • 2. SISMA Project
  • 3. GOCE-Italy
  • 4. Contents
    • Basics of C program compilation
    • How many C++ testing framework are out there?
    • Introduction to Google C++ Testing Framework (aka gtest)
    • How to download/compile/link/use gtest (live)
    • (Maybe) Some usage examples
  • 5. Hello World Example // file: hello.c #include <stdio.h> int main( int argc, char * argv[]) { printf( &quot;Hello World &quot; ); }
  • 6. Steps in C compilation C Preprocessor C Compiler Linker a.out (.exe) hello.c stdio.h hello.i hello.o (.obj) libc.a (.lib) where printf is declared where printf is defined (from /usr/lib/) (from /usr/include)
  • 7. Which testing framework?
    • According to Wikipedia [1] we have 42 framework for C++ !!
    • For comparison:
      • # of choices for C++: 42
      • # of choices for Java: 23
      • # of choices for .Net: 21
      • # of choices for Objective-C: 5
      • # of choices for C: 32
    [1] http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B
  • 8. Narrowing selection
    • The test frameworks chosen:
      • CppUnit
      • CppUnitLite
      • Google C++ Testing Framework
  • 9. CppUnit
    • Features:
      •  Created by Michael Feathers [1]
      •  Opensource
      •  API similar to JUnit
      •  The JUnit-like API doesn’t match the C++ features  longer fixtures
      •  Michael Feathers don’t use it anymore!
    [1] The author of “Working Effectively with Legacy Code”
  • 10. CppUnitLite
    • Features:
      •  Also created by Michael Feathers [1]
      •  Opensource
      •  Exploits the features of the language(s)
      •  => More easily write individual tests
      •  Michael Feathers still use it.
    [1] He decided that using CppUnit was too elaborate!
  • 11. Google C++ Testing Framework
    • Features:
      • Written by some guys at Google
      •  Writing test seems as simple as in CppUnitLite
      •  Opensource
      •  Works on many platforms:
        • Visual Studio
        • XCode
        • gcc
      •   Documentation available online
      •   Seems to be used by many people (at least on stackoverflow.com)
       
  • 12. The Winner is
    • Google C ++ Testing Framework
      • (a.k.a. gtest)
  • 13. What do you need?
    • Visual Studio 2008
    • Google Test Package (gtest-1.4.0.zip)
    • This guide 
  • 14. How test are defined
    • // the_compiler.cc
    • #include <gtest/gtest.h>
    • TEST( the_compiler , is_able_to_add ) {
    • ASSERT_EQ(2, 1+1);
    • }
    • TEST( the_compiler , knows_when_two_numbers_are_different ) {
    • ASSERT_NE(1, 2);
    • }
    • TEST( the_compiler , knows_who_comes_first ) {
    • ASSERT_LT(1, 2);
    • ASSERT_GT(2, 1);
    • }
    Suggested nomeclature: A. The system under test. B. The tested features. Note: Autodiscovery! No need of enumerating tests. 1. Include the library headers 2. Write a TEST() for each features 2.1. Write Assertions
  • 15. The Output Window
  • 16. The Optional main() function
    • Maybe you would insert a main() function which exercise all the test:
      • int main( int argc, char * argv[]) {
      • ::testing::InitGoogleTest(&argc, argv);
      • return RUN_ALL_TESTS();
      • }
    • Or you can simply add this lib: gtest_maind.lib
  • 17. How To Set Up (live) 1/2
    • Download and unzip gtest-1.4.0.zip
      • (from http:// code.google.com/p/googletest / )
    • Build solution gtest-1.4.0/msvc/gtest.sln
    • Create your testing project
    • Add this to include path:
      • gtest ­ 1.4.0/includes
    • Add these to the library path:
      • gtest ­ 1.4.0msvcgtestDebuggtest_maind.lib
      • gtest-1.4.0msvcgtestDebuggtestd.lib
  • 18. How To Set Up (live) 2/2
    • Resolve the remaining errors with some magic:
      • Resolve linking error with:
        • Use the “Multi-threaded Debug (/MTd)” option
      • Resolve the manifest error with:
        • A “clean and build” !
    • Make FAIL messages clickable with:
      • Setting
        • Build Events > Post-Build Event > Command Line
      • To value:
        • &quot;$(TargetDir)$(TargetFileName)&quot;
  • 19. Assertions
    • Most used assertion:
      • ASSERT_TRUE( condition );
      • ASSERT_FALSE( condition );
      • ASSERT_EQ( expected, actual );
      • ASSERT_NE( val1, val2 );
      • ASSERT_STREQ( expected_str ,  actual_str );
  • 20. Expectations
    • Most used expectations:
      • EXPECT_TRUE( condition );
      • EXPECT_FALSE( condition );
      • EXPECT_EQ( expected, actual );
      • EXPECT_NE( val1, val2 );
      • EXPECT_STREQ( expected_str ,  actual_str );
    • Expectations vs Assertions:
      • If an assertion fails the TEST execution stops
      • If an expectation fails the TEST execution continues
  • 21. References
    • Feathers, Michael C (2004). 
    • Working Effectively with Legacy Code.
    • Prentice Hall.  ISBN   0-13-117705-2 .
    • Exploring the C++ Unit Testing Framework Jungle
      • http://gamesfromwithin.com/exploring-the-c-unit-testing-framework-jungle
    • Compiler, Assembler, Linker and Loader: a brief story:
      • http://www.tenouk.com/ModuleW.html (spiega pure il formato ELF!)
    • Google C++ Testing Framework
      • http:// code.google.com/p/googletest /
    • Tutorial Google Test con Visual Studio 2008
      • http://blog.andreafrancia.it/2010/04/tutorial-google-test_01.html
  • 22.
    • Grazie dell’ascolto!
    • (soprattutto alla mia fidanzata ;)