Upload
andrea-francia
View
7.052
Download
8
Tags:
Embed Size (px)
DESCRIPTION
How to set up the C++ xUnit framework created by Google in Visual Studio 2008.
Citation preview
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/
SISMA Project
GOCE-Italy
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
Hello World Example
// file: hello.c
#include <stdio.h>
int main(int argc, char * argv[]) {
printf("Hello World\n");}
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)
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
Narrowing selection
• The test frameworks chosen:– CppUnit – CppUnitLite – Google C++ Testing Framework
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”
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!
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)
The Winner is
• Google C++ Testing Framework– (a.k.a. gtest)
What do you need?
• Visual Studio 2008
• Google Test Package (gtest-1.4.0.zip)
• This guide
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.
The Output Window
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
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
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)"
Assertions
• Most used assertion:– ASSERT_TRUE(condition);– ASSERT_FALSE(condition);– ASSERT_EQ(expected, actual);– ASSERT_NE(val1, val2);– ASSERT_STREQ(expected_str, actual_str);
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
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
Grazie dell’ascolto!(soprattutto alla mia fidanzata ;)