Using Google Test with Visual Studio 2008 Andrea Francia (Galileian Plus) XPUG (14 aprile 2010) http:// www.galileianplus....
SISMA Project
GOCE-Italy
Contents <ul><li>Basics of C program compilation </li></ul><ul><li>How many C++ testing framework are out there? </li></ul...
Hello World Example // file: hello.c #include   <stdio.h> int  main( int  argc,  char  * argv[]) { printf( &quot;Hello Wor...
Steps in C compilation C Preprocessor  C Compiler  Linker a.out  (.exe) hello.c stdio.h hello.i hello.o  (.obj) libc.a  (....
Which testing framework? <ul><li>According to Wikipedia [1] we have 42 framework for C++ !! </li></ul><ul><li>For comparis...
Narrowing selection <ul><li>The test frameworks chosen: </li></ul><ul><ul><li>CppUnit  </li></ul></ul><ul><ul><li>CppUnitL...
CppUnit <ul><li>Features: </li></ul><ul><ul><li>   Created by Michael Feathers  [1] </li></ul></ul><ul><ul><li>   Openso...
CppUnitLite <ul><li>Features: </li></ul><ul><ul><li>   Also created by Michael Feathers  [1] </li></ul></ul><ul><ul><li>...
Google C++ Testing Framework <ul><li>Features: </li></ul><ul><ul><li>Written by some guys at Google </li></ul></ul><ul><ul...
The Winner is <ul><li>Google C ++  Testing Framework </li></ul><ul><ul><li>(a.k.a. gtest) </li></ul></ul>
What do you need? <ul><li>Visual Studio 2008  </li></ul><ul><li>Google Test Package (gtest-1.4.0.zip) </li></ul><ul><li>Th...
How test are defined <ul><li>// the_compiler.cc </li></ul><ul><li>#include <gtest/gtest.h> </li></ul><ul><li>TEST( the_com...
The Output Window
The Optional main() function <ul><li>Maybe you would insert a  main()  function which exercise all the test: </li></ul><ul...
How To Set Up (live) 1/2 <ul><li>Download and unzip gtest-1.4.0.zip </li></ul><ul><ul><li>(from  http:// code.google.com/p...
How To Set Up (live) 2/2 <ul><li>Resolve the remaining errors with some magic: </li></ul><ul><ul><li>Resolve linking error...
Assertions <ul><li>Most used assertion: </li></ul><ul><ul><li>ASSERT_TRUE( condition ); </li></ul></ul><ul><ul><li>ASSERT_...
Expectations <ul><li>Most used expectations: </li></ul><ul><ul><li>EXPECT_TRUE( condition ); </li></ul></ul><ul><ul><li>EX...
References <ul><li>Feathers, Michael C (2004).  </li></ul><ul><li>Working Effectively with Legacy Code.  </li></ul><ul><li...
<ul><li>Grazie dell’ascolto! </li></ul><ul><li>(soprattutto alla mia fidanzata ;) </li></ul>
Upcoming SlideShare
Loading in...5
×

Google C++ Testing Framework in Visual Studio 2008

7,159

Published on

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

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
7,159
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
130
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Google C++ Testing Framework in Visual Studio 2008

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

    Clipping is a handy way to collect important slides you want to go back to later.

×