c++ unit test with google testing framework

52
What is a unit test? How to get started ? GTest - basics Make code testable General tips References Writing Unit Tests with Google Testing Framework Humberto Cardoso Marchezi [email protected] May 31, 2015

Upload: humberto-marchezi

Post on 28-Jul-2015

181 views

Category:

Software


6 download

TRANSCRIPT

Page 1: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Writing Unit Tests with Google Testing Framework

Humberto Cardoso [email protected]

May 31, 2015

Page 2: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Table of Contents

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 3: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 4: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Test levels

Page 5: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Unit tests characteristics

Fast

Isolated

Repeatable

Self-verifying

Timely

Page 6: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 7: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

We will follow the steps:

1 Identify test cases

2 Share test cases with others

3 Write one test case as a unit test

4 Watch test failing

5 Write just enough code to pass test

6 Clean up code

7 Go to step 3 until all tests are done

Page 8: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Identify test cases

Problem: implement function for factorial operation5! = 5 x 4 x 3 x 2 x 1 = 1204! = 4 x 3 x 2 x 1 = 240! = 1

test case expected behavior

Page 9: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Identify test cases

Problem: implement function for factorial operation

test case expected behaviorsmall positive numbers should calculate factorial

input value is 0 should return 1

Page 10: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Share test cases with others

Problem: implement function for factorial operation

test case expected behaviorsmall positive numbers should calculate factorial

input value is 0 should return 1

Page 11: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Factorial table

n factorial(n)0 11 12 23 64 245 1206 7207 50408 403209 362880

10 362880011 3991680012 47900160020 2432902008176640000

Page 12: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Share test cases with others

Problem: implement function for factorial operation

test case expected behaviorsmall positive numbers should calculate factorial

input value is 0 should return 1

input value is negative return error codeinput value is a biggerpositive integer

return overflow error code

Page 13: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Write one test case as a unit test

test case expected behaviorinput value is 0 should return 1

TEST( Fa c t o r i a l T e s t , Fac to r i a lOfZe roShou ldBeOne ){

ASSERT EQ(1 , f a c t o r i a l ( 0 ) ) ; // 0 ! == 1}

Page 14: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Write just enough code to pass test

For a test like this:

TEST( Fa c t o r i a l T e s t , Fac to r i a lOfZe roShou ldBeOne ){

ASSERT EQ(1 , f a c t o r i a l ( 0 ) ) ; // 0 ! == 1}

Focus on code to ONLY make it pass like this:

i n t f a c t o r i a l ( i n t n ){

r e t u r n 1 ;}

Page 15: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Clean up code

Clean up accumulated mess

Refactorings

Optimizations

Readability

Modularity

Don’t miss this step!

Page 16: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Go to next test

TEST( Fa c t o r i a l T e s t , F a c tO f Sma l l P o s i t i v e I n t e g e r s ){

ASSERT EQ(1 , f a c t o r i a l ( 1 ) ) ;ASSERT EQ(24 , f a c t o r i a l ( 4 ) ) ;ASSERT EQ(120 , f a c t o r i a l ( 5 ) ) ;

}

Page 17: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 18: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Why ?

Portable: Works for Windows, Linux and Mac

Familiar: Similar to other xUnit frameworks (JUnit, PyUnit,etc.)

Simple: Unit tests are small and easy to read

Popular: Many users and available documentation

Powerful: Allows advanced configuration if needed

Page 19: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Tests

TEST ( ) macro defines a test name and function

TEST( t e s t ca s e name , t e s t name ){

. . . t e s t body . . .}

Example:

TEST( F ibonacc iTe s t , Ca l c u l a t eS e edVa l u e s ){

ASSERT EQ(0 , F i b ona c c i ( 0 ) ) ;ASSERT EQ(1 , F i b ona c c i ( 1 ) ) ;

}

Page 20: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Assertions

Checks expected behavior for function or method

ASSERT * fatal failures (test is interrupted)

EXPECT * non-fatal failures

Operator <<custom failure message

ASSERT EQ( x . s i z e ( ) , y . s i z e ( ) ) <<” Vec to r s x and y a r e o f unequa l l e n g t h ” ;

f o r ( i n t i = 0 ; i < x . s i z e ( ) ; ++i ){

EXPECT EQ( x [ i ] , y [ i ] ) <<” Vec to r s x and y d i f f e r a t i nd ex ” << i ;

}

Page 21: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Assertions

Logic assertions

Description Fatal assertion Non-fatal assertionTrue condition ASSERT TRUE(condition); EXPECT TRUE(condition);False condition ASSERT FALSE(condition); EXPECT FALSE(condition);

Integer assertions

Description Fatal assertion Non-fatal assertionEqual ASSERT EQ(expected, actual); EXPECT EQ(expected, actual);Less then ASSERT LT(expected, actual); EXPECT LT(expected, actual);Less or equalthen

ASSERT LE(expected, actual); EXPECT LE(expected, actual);

Greater then ASSERT GT(expected, actual); EXPECT GT(expected, actual);Greater orequal then

ASSERT GE(expected, actual); EXPECT GE(expected, actual);

Page 22: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Assertions

String assertions

Description Fatal assertion Non-fatal assertionDifferent content ASSERT STRNE(exp, actual); EXPECT STREQ(exp, actual);Same content, ig-noring case

ASSERT STRCASEEQ(exp,actual); EXPECT STRCASEEQ(exp,actual);

Different content,ignoring case

ASSERT STRCASENE(exp,actual); EXPECT STRCASEEQ(exp,actual);

Explicit assertions

Description AssertionExplicit success SUCCEED();Explicit fatal failure FAIL();Explicit non-fatal failure ADD FAILURE();Explicit non-fatal failurewith detailed message

ADD FAILURE AT(”file path”,linenumber);

Page 23: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Assertions

Exception assertionsDescription Fatal assertion Non-fatal assertionException typewas thrown

ASSERT THROW(statement,exception); EXPECT THROW(statement, exception);

Any exceptionwas thrown

ASSERT ANY THROW(statement); EXPECT ANY THROW(statement);

No exceptionthrown

ASSERT NO THROW(statement); EXPECT NO THROW(statement);

Floating point number assertionsDescription Fatal assertion Non-fatal assertionDouble compari-son

ASSERT DOUBLE EQ(expected, actual); EXPECT DOUBLE EQ(expected, actual);

Float comparison ASSERT FLOAT EQ(expected, actual); EXPECT FLOAT EQ(expected, actual);Float point com-parison with mar-gin of error

ASSERT NEAR(val1, val2, abse rror); EXPECT NEAR(val1, val2, abse rror);

Page 24: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Test fixtures

Definition:

Tests that operate on similar data

c l a s s LogTests : p u b l i c : : t e s t i n g : : Test {p r o t e c t e d :vo i d SetUp ( ) { . . . } // Prepa re data f o r each t e s tvo i d TearDown ( ) { . . . } // Re l e a s e data f o r each t e s ti n t auxMethod ( . . . ) { . . . }

}

TEST F ( LogTests , c l e a n L o gF i l e s ) {. . . t e s t body . . .

}

Page 25: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Controlling execution

Run all tests

mytestprogram . exe

Runs ”SoundTest” test cases

mytestprogram . exe − g t e s t f i l t e r=SoundTest .∗

Runs tests whose name contains ”Null” or ”Constructor”

mytestprogram . exe − g t e s t f i l t e r =∗Nu l l ∗ :∗ Con s t r u c t o r∗

Runs tests except those whose preffix is ”DeathTest”

mytestprogram . exe − g t e s t f i l t e r=−∗DeathTest .∗

Run ”FooTest” test cases except ”testCase3”

mytestprogram . exe − g t e s t f i l t e r=FooTest .∗−FooTest .t e s tCa s e3

Page 26: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Controlling execution

Temporarily Disabling Tests

For test cases, add DISABLED preffix

TEST( SoundTest , DISABLED legacyProcedure ) { . . . }

For all tests cases in test fixture, add DISABLED to test fixture name

c l a s s DISABLED VectTest : p u b l i c : : t e s t i n g : : Test{ . . . } ;TEST F (DISABLED ArrayTest , t e s tCa s e4 ) { . . . }

Enabling execution of disabled tests

mytest . exe −−g t e s t a l s o r u n d i s a b l e d t e s t s

Page 27: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Output customization

Invoking tests with output customization

mytest program . exe −−g t e s t o u t p u t =”xml : d i r e c t o r y \my f i l e . xml”

XML format (JUnit compatible)

< t e s t s u i t e s name=” A l l T e s t s ” . . .>< t e s t s u i t e name=” t e s t c a s e name ” . . .>

<t e s t c a s e name=” tes t name ” . . .>< f a i l u r e message=” . . . ”/>< f a i l u r e message=” . . . ”/>< f a i l u r e message=” . . . ”/>

</ t e s t c a s e></ t e s t s u i t e>

</ t e s t s u i t e s>

Page 28: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Output customization

Listing test names (without executing them)

mytest program . exe −−g t e s t l i s t t e s t s

TestCase1 .TestName1TestName2

TestCase2 .TestName

Page 29: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Advanced features

Predicate assertions

AssertionResult

Predicate formatter

Windows HRESULT assertions

Type assertions

Customizing value type serialization

Death tests

Logging additional information in XML output

Value parametized tests Etc ...

Page 30: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 31: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Types of functions

Testing functions

From testing perspective, there are mainly 3 types of functions:

functions with input and output.Ex: math functions

functions with input and object side-effect.Ex: person.setName(’John’);

functions with input and not testable side-effect.Ex: drawLine(30,30,120,190);

Page 32: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Types of functions

Functions with input and output

the easiest case - direct implementation

functions should be refactored to this format when possible

TEST( F ibonacc iTe s t , F ibonacc iOfZeroShou ldBeZero ) {ASSERT EQ(0 , f i b o n a c c i ( 0 ) ) ;

}TEST( F ibonacc iTe s t , FibonacciOfOneShouldBeOne ) {

ASSERT EQ(1 , f i b o n a c c i ( 1 ) ) ;}TEST( F ibonacc iTe s t , F i b o n a c c iO f Sma l l P o s i t i v e I n t e g e r s ) {

ASSERT EQ(2 , f i b o n a c c i ( 3 ) ) ;ASSERT EQ(3 , f i b o n a c c i ( 4 ) ) ;

}

Page 33: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Types of functions

Functions with input and object side-effect

not so direct but still easy

object should contain a read method to check state

TEST( PersonTest , SetB i r thDateShou ldMod i fyAge ) {Person p ;p . s e tB i r t hDa t e ( ”1978−09−16” ) ;ASSERT STR EQ(36 , p . age ( ) ) ;

}

Page 34: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Types of functions

Functions with input and not testable side-effect

can’t test what we can’t measure (in code)

effort is not worth for unit testing

TEST( D i sp l ayTes t , L i n eShou ldBeD i sp l ayed ) {D i s p l a y d i s p l a y ;d i s p l a y . drawLine (20 ,34 ,100 ,250) ;??????????? − How to a s s e r t t h i s ???

}

Page 35: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Configuration and implementation

Isolate configuration from the actual code

How to test Init failure in this code ?

c l a s s Component {p u b l i c :

i n t i n i t ( ) {i f ( XYZ License ( ” key abcde f g ” ) != 0) {

r e t u r n INIT FAILURE ;}r e t u r n INIT SUCCESS ;

}} ;

’init’ is a function without testable side-effects

Page 36: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Configuration and implementation

Isolate configuration from the actual code

Instead separate configuration from code

c l a s s Component{

p r o t e c t e d :s t d : : s t r i n g l i c e n s e = ” key abcde f g ” ;

p u b l i c :i n t i n i t ( ) {

i f ( XYZ License ( l i c e n s e ) == FAILURE){

r e t u r n INIT FAILURE ;}

}} ;

License information became an object variable

Page 37: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Configuration and implementation

Isolate configuration from the actual code

Extend class to add methods to access protected information

c l a s s TestableComponent : p u b l i c Component{

p u b l i c :v o i d s e t L i c e n s e ( s t r i n g l i c e n s e ) {

t h i s . l i c e n s e = l i c e n s e ;}

} ;

’setLicense’ is necessary to modify the state of ’init’ method

Page 38: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Configuration and implementation

Isolate configuration from the actual code

Now the test becomes possible:

TEST( ComponentTest , U p o n I n i t i a l i z a t i o n F a i l u r e S h o u l dReturn Er rorCode )

{// A u x i l i a r t e s t c l a s s can be d e c l a r e d on l y// i n the scope o f the t e s tc l a s s TestableComponent : p u b l i c Component{

p u b l i c :v o i d s e t L i c e n s e ( s t r i n g l i c e n s e ) {

t h i s . l i c e n s e = l i c e n s e ;}

} ;TestableComponent component ;component . s e t L i c e n s e ( ”FAKE LICENSE” ) ;ASSERT EQ( INIT FAILURE , component . i n i t ( ) ) ;

} ;

Page 39: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Extract your application

Separate app logic from presentation

Presentation logic refers to UI components to show userfeedback

Application logic refers to data transformation upon user orother system input

Presentation and logic should be separated concerns

Presentation is usually not unit testable

Application logic is usually testable

s t d : : s t r i n g name = u i . txtName . t e x t ( ) ;s t d : : s i z e t p o s i t i o n = name . f i n d ( ” ” ) ;s t d : : s t r i n g f i r s tName = name . s u b s t r (0 , p o s i t i o n ) ;s t d : : s t r i n g lastName = name . s u b s t r ( p o s i t i o n ) ;u i . l bD e s c r i p t i o n . s e tTex t ( lastName + ” , ” + f i r s tName ) ;

Page 40: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

Extract your application

Separate app logic from presentation

Extract app logic in a separated function (preferrebly class)

s t d : : s t r i n g name = u i . txtName . t e x t ( ) ;s t d : : s t r i n g d e s c r i p t i o n = app . makeDesc r i p t i on (name) ;u i . l bD e s c r i p t i o n . s e tTex t ( d e s c r i p t i o n ) ;

TEST( App l i c a t i o nTe s t , s hou ldMakeDesc r i p t i on ) {App l i c a t i o n app ;s t d : : s t r i n g d e s c r i p t i o n ;d e s c r i p t i o n = app . makeDesc r i p t i on ( ”James O l i v e r ” ) ;ASSERT STREQ( ” O l i v e r , James” , d e s c r i p t i o n ) ;

}

Page 41: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Code with mixed responsibilities

HttpResponse r e s pon s e ;r e s pon s e = ht tp . ge t ( ” h t tp : // domain/ ap i / c on t r y c od e ” +

”? token=” + token ) ;s t d : : s t r i n g countryCode = r e spon s e . body ;s t d : : s t r i n g langCode ;i f ( countryCode == BRAZIL) {

langCode = PORTUGUESE;} e l s e i f ( countryCode == USA) {

langCode = ENGLISH ;} e l s e {

langCode = ENGLISH ;}u i . se tLanguage ( langCode ) ;

Page 42: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Identify and isolate dependency

WebAPIGateway webAPI ;s t d : : s t r i n g countryCode=webAPI . use rCount ryCode ( token ) ;LanguageHe lper l anguage ;s t d : : s t r i n g langCode=language . f i ndLang ( countryCode ) ;u i . s e tLanguage ( langCode ) ;

TEST( LanguageHelperTest , shou ldF indLanguageForCount ry ){

LanguageHe lper l anguage ;ASSERT EQ(PORTUGUESE, l anguage . f i ndLang (BRAZIL) ) ;ASSERT EQ(ENGLISH , l anguage . f i ndLang (USA) ) ;ASSERT EQ(ENGLISH , l anguage . f i ndLang (JAPAN) ) ;. . .

}

Page 43: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Fake objects

#i n c l u d e ”gmock/gmock . h”TEST(WebAPIGatewayTest , shou ldF indUse rCount r y ) {

c l a s s HttpRequestFake : p u b l i c HttpRequest{

s t d : : s t r i n g use rCount ryCode ( s td : : s t r i n g token ) {s t d : : s t r i n g coun t r y = ”” ;i f ( token==”abc” ) coun t r y = BRAZIL ;r e t u r n coun t r y ;

}} ;HttpRequest ∗ httpFake = new HttpRequestFake ( ) ;WebAPIGateway ap i = WebAPIGateway ( ht tpFake ) ;s t d : : s t r i n g countryCode = ap i . use rCount ryCode ( ”abc” ) ;d e l e t e ht tpFake ;ASSERT EQ(BRAZIL , countryCode ) ;

}

Page 44: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Mock objects

#i n c l u d e ”gmock/gmock . h”c l a s s HttpRequestMock : p u b l i c HttpRequest{

MOCK CONST METHOD1( get ( ) , s t d : : s t r i n g ( s td : : s t r i n g ) ) ;} ;TEST(WebAPIGatewayTest , shou ldF indUse rCount r y ) {

HttpRequest ∗ httpMock = new HttpRequestMock ( ) ;WebAPIGateway ap i = WebAPIGateway ( httpMock ) ;EXPECT CALL( httpMock ,

ge t ( ” h t tp : // domain/ ap i / c on t r y c od e ? token=abc” ). Times (1 ). Wi l lOnce ( Return ( Response (BRAZIL) ) ) ;

s t d : : s t r i n g countryCode = ap i . use rCount ryCode ( ”abc” ) ;d e l e t e httpMock ;ASSERT EQ(BRAZIL , countryCode ) ;

}

Page 45: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Code with dependencies

How to test vertices coordinates calculation ?

vo i d drawQuad ( f l o a t cx , f l o a t cy , f l o a t s i d e ){

f l o a t h a l f S i d e = s i d e / 2 . 0 ;g lBeg i n (GL LINE LOOP) ;

g l V e r t e x 3 f ( cx−h a l f S i d e , cy−h a l f S i d e ) ;g l V e r t e x 3 f ( cx+ha l f S i d e , cy−h a l f S i d e ) ;g l V e r t e x 3 f ( cx+ha l f S i d e , cy+h a l f S i d e ) ;g l V e r t e x 3 f ( cx−h a l f S i d e , cy+h a l f S i d e ) ;

g lEnd ( ) ;}

Page 46: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Identify and isolate dependency

vo i d drawQuad ( f l o a t cx , f l o a t cy , f l o a t s i d e ,I D i s p l a y ∗ d i s p l a y {f l o a t h a l f S i d e = s i d e / 2 . 0 ;s t d : : v e c to r<f l o a t> v e r t i c e s = {cx−h a l f S i d e , cy−h a l f S i d e ,cx+ha l f S i d e , cy−h a l f S i d e ,cx+ha l f S i d e , cy+ha l f S i d e ,cx−h a l f S i d e , cy+h a l f S i d e} ;d i s p l a y−>r e c t a n g l e ( v e r t i c e s ) ;

}

c l a s s D i s p l a y : p u b l i c I D i s p l a y {p u b l i c : v o i d r e c t a n g l e ( s t d : : v e c to r<f l o a t> v e r t i c e s ) {

g lBeg i n (GL LINE LOOP) ;g l V e r t e x 3 f ( v e r t i c e s [ 0 ] , v e r t i c e s [ 1 ] ) ;. . .

g lEnd ( ) ;} } ;

Page 47: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Fake objects

TEST( DrawTests , d r awQuadShou l dCa l cu l a t eVe r t i c e s ) {c l a s s FakeD i sp l a y : p u b l i c D i s p l a y {

p r o t e c t e d : s t d : : v e c to r<f l o a t> v e r t i c e s ;p u b l i c :

v o i d r e c t a n g l e ( s t d : : v e c to r<f l o a t> v e r t i c e s ) {v e r t i c e s = v e r t i c e s ;

}s t d : : v e c to r<f l o a t> v e r t i c e s ( ) {

r e t u r n v e r t i c e s ;}

} ;I D i s p l a y ∗ d i s p l a y = new FakeD i sp l ay ( ) ;drawQuad ( 1 0 . 0 , 1 0 . 0 , 1 0 . 0 , d i s p l a y ) ;ASSERT VECT EQ({ 5 . 0 , 5 . 0 , 1 5 . 0 , 5 . 0 , 1 5 . 0 , 1 5 . 0 , 5 . 0 , 1 5 . 0 } ,d i s p l a y−>v e r t i c e s ( ) ) ;

}

Page 48: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

External dependencies

Mock objects

#i n c l u d e ”gmock/gmock . h”TEST( DrawTests , d r awQuadShou l dCa l cu l a t eVe r t i c e s ) {

c l a s s MockDisplay : p u b l i c D i s p l a y {MOCKMETHOD1( r e c t a n g l e , v o i d ( s td : : v e c to r<f l o a t >) ;} ;I D i s p l a y ∗ d i s p l a y = new MockDisplay ( ) ;EXPECT CALL( d i s p l a y ,

r e c t a n g l e ( { 5 . 0 , 5 . 0 , 1 5 . 0 , 5 . 0 , 1 5 . 0 , 1 5 . 0 , 5 . 0 , 1 5 . 0 } ). Times (1 )

drawQuad ( 1 0 . 0 , 1 0 . 0 , 1 0 . 0 , d i s p l a y ) ;}

Page 49: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 50: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

General tips

Test behavior not implementation

There is no behavior in non-public methods

Use name convention for tests to improve readability

Test code should be as good as production code - it is notsketch code

GTest alone can’t help you

Workflow is important - talk to your QA

Improve modularization to avoid being trapped by nontestable code

Use mock objects with caution

Understand the difference between fake and mock objects

Page 51: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

1 What is a unit test?2 How to get started ?3 GTest - basics

Why ?TestsAssertionsTest fixturesControlling executionOutput customization

Advanced features4 Make code testable

Types of functionsConfiguration andimplementationExtract your applicationExternal dependencies

5 General tips6 References

Page 52: C++ Unit Test with Google Testing Framework

What is a unit test? How to get started ? GTest - basics Make code testable General tips References

General tips

Getting Started with Google Testing Frameworkhttps://code.google.com/p/googletest/wiki/Primer

GTest Advanced Guidehttps://code.google.com/p/googletest/wiki/AdvancedGuide

Clean Code: A Handbook of Agile Software CraftsmanshipRobert C. Martin - Prentice Hall PTR, 2009

Google C++ Mocking Framework for Dummieshttps://code.google.com/p/googlemock/wiki/ForDummies