C++ Unit Test with Google Testing Framework

Download C++ Unit Test with Google Testing Framework

Post on 28-Jul-2015

148 views

Category:

Software

4 download

Embed Size (px)

TRANSCRIPT

<p> 1. 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 hcmarchezi@gmail.com May 31, 2015 2. 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 ? Tests Assertions Test xtures Controlling execution Output customization Advanced features 4 Make code testable Types of functions Conguration and implementation Extract your application External dependencies 5 General tips 6 References 3. 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 ? Tests Assertions Test xtures Controlling execution Output customization Advanced features 4 Make code testable Types of functions Conguration and implementation Extract your application External dependencies 5 General tips 6 References 4. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Test levels 5. 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 6. 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 ? Tests Assertions Test xtures Controlling execution Output customization Advanced features 4 Make code testable Types of functions Conguration and implementation Extract your application External dependencies 5 General tips 6 References 7. 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 8. 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 5! = 5 x 4 x 3 x 2 x 1 = 120 4! = 4 x 3 x 2 x 1 = 24 0! = 1 test case expected behavior 9. 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 behavior small positive numbers should calculate factorial input value is 0 should return 1 10. 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 behavior small positive numbers should calculate factorial input value is 0 should return 1 11. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Factorial table n factorial(n) 0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 10 3628800 11 39916800 12 479001600 20 2432902008176640000 12. 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 behavior small positive numbers should calculate factorial input value is 0 should return 1 input value is negative return error code input value is a bigger positive integer return overow error code 13. 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 behavior input value is 0 should return 1 TEST( F a c t o r i a l T e s t , FactorialOfZeroShouldBeOne ) { ASSERT EQ(1 , f a c t o r i a l (0) ) ; // 0! == 1 } 14. 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( F a c t o r i a l T e s t , FactorialOfZeroShouldBeOne ) { 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; } 15. 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 Dont miss this step! 16. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Go to next test TEST( F a c t o r i a l T e s t , F a c t O f S m a 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) ) ; } 17. 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 ? Tests Assertions Test xtures Controlling execution Output customization Advanced features 4 Make code testable Types of functions Conguration and implementation Extract your application External dependencies 5 General tips 6 References 18. 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 conguration if needed 19. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Tests TEST ( ) macro denes a test name and function TEST( test case name , test name ) { . . . t e s t body . . . } Example: TEST( FibonacciTest , CalculateSeedValues ) { ASSERT EQ(0 , Fibonacci (0) ) ; ASSERT EQ(1 , Fibonacci (1) ) ; } 20. 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 &lt; t e s t c a s e&gt; t e s t s u i t e&gt; t e s t s u i t e s&gt; 28. 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 . TestName1 TestName2 TestCase2 . TestName 29. 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 ... 30. 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 ? Tests Assertions Test xtures Controlling execution Output customization Advanced features 4 Make code testable Types of functions Conguration and implementation Extract your application External dependencies 5 General tips 6 References 31. 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-eect. Ex: person.setName(John); functions with input and not testable side-eect. Ex: drawLine(30,30,120,190); 32. 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( FibonacciTest , FibonacciOfZeroShouldBeZero ) { ASSERT EQ(0 , f i b o n a c c i (0) ) ; } TEST( FibonacciTest , FibonacciOfOneShouldBeOne ) { ASSERT EQ(1 , f i b o n a c c i (1) ) ; } TEST( FibonacciTest , F i b o n a c c i O f S m a 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) ) ; } 33. 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-eect not so direct but still easy object should contain a read method to check state TEST( PersonTest , SetBirthDateShouldModifyAge ) { Person p ; p . setBirthDate ( 19780916 ) ; ASSERT STR EQ(36 , p . age () ) ; } 34. 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-eect cant test what we cant measure (in code) eort is not worth for unit testing TEST( DisplayTest , LineShouldBeDisplayed ) { Display 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 ??? } 35. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Conguration and implementation Isolate conguration 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 abcdefg ) != 0) { r e t u r n INIT FAILURE ; } r e t u r n INIT SUCCESS ; } }; init is a function without testable side-eects 36. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Conguration and implementation Isolate conguration from the actual code Instead separate conguration from code c l a s s Component { p r o t e c t e d : std : : s t r i n g l i c e n s e = key abcdefg ; 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 37. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Conguration and implementation Isolate conguration 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 : void 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 38. What is a unit test? How to get started ? GTest - basics Make code testable General tips References Conguration and implementation Isolate conguration 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 d Return ErrorCode ) { // 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 only // i n the scope of the t e s t c l a s s TestableComponent : p u b l i c Component { p u b l i c : void 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 () ) ; }; 39. 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 user feedback Application logic refers to data transformation upon user or other system input Presentation and logic should be separated concerns Presentation is usually not unit testable Application logic is usually testable std : : s t r i n g name = u i . txtName . t e x t () ; std : : s i z e t p o s i t i o n = name . f i n d ( ) ; std : : s t r i n g firstName = name . s u b s t r (0 , p o s i t i o n ) ; std : : 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 b D e s c r i p t i o n . setText ( lastName + , + firstName ) ; 40. 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) std : : s t r i n g name = u i . txtName . t e x t () ; std : : s t r i n g d e s c r i p t i o n = app . makeDescription (name) ; u i . l b D e s c r i p t i o n . setText ( d e s c r i p t i o n ) ; TEST( ApplicationTest , shouldMakeDescription ) { A p p l i c a t i o n app ; std : : 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 . makeDescription ( James O l i v e r ) ; ASSERT STREQ( Oliver , James , d e s c r i p t i o n ) ; } 41. What is a unit test? How to get started ? GTest - basics Make code testable General tips References External dependencies Code with mixed responsibilities HttpResponse response ; response = http . get ( http :// domain/ api / contry code + ? token= + token ) ; std : : s t r i n g countryCode = response . body ; std : : 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 . setLanguage ( langCode ) ; 42. 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 ; std : : s t r i n g countryCode=webAPI . userCountryCode ( token ) ; LanguageHelper language ; std : : s t r i n g langCode=language . findLang ( countryCode ) ; u i . setLanguage ( langCode ) ; TEST( LanguageHelperTest , shouldFindLanguageForCountry ) { LanguageHelper language ; ASSERT EQ(PORTUGUESE, language . findLang (BRAZIL) ) ; ASSERT EQ(ENGLISH , language . findLang (USA) ) ; ASSERT EQ(ENGLISH , language . findLang (JAPAN) ) ; . . . } 43. 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 , shouldFindUserCountry ) { c l a s s HttpRequestFake : p u b l i c HttpRequest { std : : s t r i n g userCountryCode ( std : : s t r i n g token ) { std : : s t r i n g country = ; i f ( token==abc ) country = BRAZIL ; r e t u r n country ; } }; HttpRequest httpFake = new HttpRequestFake () ; WebAPIGateway api = WebAPIGateway ( httpFake ) ; std : : s t r i n g countryCode = api . userCountryCode ( abc ) ; d e l e t e httpFake ; ASSERT EQ(BRAZIL , countryCode ) ; } 44. 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 () , std : : s t r i n g ( std : : s t r i n g ) ) ; }; TEST( WebAPIGatewayTest , shouldFindUserCountry ) { HttpRequest httpMock = new HttpRequestMock ()...</p>