google c++ testing framework in visual studio 2008

22
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/

Upload: andrea-francia

Post on 11-May-2015

7.049 views

Category:

Technology


8 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Google C++ Testing Framework in Visual Studio 2008

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/

Page 2: Google C++ Testing Framework in Visual Studio 2008

SISMA Project

Page 3: Google C++ Testing Framework in Visual Studio 2008

GOCE-Italy

Page 4: Google C++ Testing Framework in Visual Studio 2008

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

Page 5: Google C++ Testing Framework in Visual Studio 2008

Hello World Example

// file: hello.c

#include <stdio.h>

int main(int argc, char * argv[]) {

printf("Hello World\n");}

Page 6: Google C++ Testing Framework in Visual Studio 2008

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)

Page 7: Google C++ Testing Framework in Visual Studio 2008

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

Page 8: Google C++ Testing Framework in Visual Studio 2008

Narrowing selection

• The test frameworks chosen:– CppUnit – CppUnitLite – Google C++ Testing Framework

Page 9: Google C++ Testing Framework in Visual Studio 2008

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”

Page 10: Google C++ Testing Framework in Visual Studio 2008

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!

Page 11: Google C++ Testing Framework in Visual Studio 2008

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)

   

Page 12: Google C++ Testing Framework in Visual Studio 2008

The Winner is

• Google C++ Testing Framework– (a.k.a. gtest)

Page 13: Google C++ Testing Framework in Visual Studio 2008

What do you need?

• Visual Studio 2008

• Google Test Package (gtest-1.4.0.zip)

• This guide

Page 14: Google C++ Testing Framework in Visual Studio 2008

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);

}

1. Include the library headers

2. Write a TEST() for each features

2.1. Write Assertions

Suggested nomeclature:

A. The system under test.

B. The tested features.

Note: Autodiscovery! No need of enumerating tests.

Page 15: Google C++ Testing Framework in Visual Studio 2008

The Output Window

Page 16: Google C++ Testing Framework in Visual Studio 2008

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

Page 17: Google C++ Testing Framework in Visual Studio 2008

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.0\msvc\gtest\Debug\gtest_maind.lib– gtest-1.4.0\msvc\gtest\Debug\gtestd.lib

Page 18: Google C++ Testing Framework in Visual Studio 2008

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:• "$(TargetDir)\$(TargetFileName)"

Page 19: Google C++ Testing Framework in Visual Studio 2008

Assertions

• Most used assertion:– ASSERT_TRUE(condition);– ASSERT_FALSE(condition);– ASSERT_EQ(expected, actual);– ASSERT_NE(val1, val2);– ASSERT_STREQ(expected_str, actual_str);

Page 20: Google C++ Testing Framework in Visual Studio 2008

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

Page 21: Google C++ Testing Framework in Visual Studio 2008

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

Page 22: Google C++ Testing Framework in Visual Studio 2008

Grazie dell’ascolto!(soprattutto alla mia fidanzata ;)