Unit Tests Руслан Трифонов Omegasoft Ltd.
Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns 2.3. Data driven patterns 2.4. Data transaction patterns 2.5. Performance patterns 2.6. Process patterns 2.7. Simulation patterns 2.8. Stress test patterns 3.Ресурси
Definition A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested.
Scope Design Development Test and Fix Time flow Planning Unit Testing System Testing Integration Testing Functional Testing Release Testing Unit Tests and Application Lifecycle
Unit test framework is needed.Unit test framework is needed. Unit tests are written by developers for developersUnit tests are written by developers for developers Unit tests are released into the code repository along with the code they testUnit tests are released into the code repository along with the code they test Some facts
Trivial code may be omitted (property getters and setters)Trivial code may be omitted (property getters and setters) Ideally all unit tests should pass before check-inIdeally all unit tests should pass before check-in More facts
Code and TestCode and Test Test FirstTest First Unit Testing Approach
Write code Write unit test Run and succeed Code and Test Time flow
Pick а test Compile and Fail Write code to pass test Write enough code to compile Run test and fail Create a test list Test Driven Development Time flow Write test Remove duplication
Why TDD? Helps find design issues early and avoids reworkHelps find design issues early and avoids rework Writing code to satisfy a test is a focused activity – less chance of errorWriting code to satisfy a test is a focused activity – less chance of error Tests will be a more comprehensive than when written after codeTests will be a more comprehensive than when written after code
Unit tests improve designUnit tests improve design Unit tests are good documentationUnit tests are good documentation Unit tests enable collective code ownershipUnit tests enable collective code ownership Why Unit Tests?
Unit tests decrease the defect-injection rate due to refactoring / changesUnit tests decrease the defect-injection rate due to refactoring / changes Unit tests decrease the cost of the changesUnit tests decrease the cost of the changes Unit tests bring higher development velocity in the latest dev. stagesUnit tests bring higher development velocity in the latest dev. stages Unit tests enable frequent integrationUnit tests enable frequent integration
Development Workstation Source Code Repository Server Integration Server Publish Results Continuous Integration Check out Build Perform BVT Publish results Check-in
Why Continuous Integration? The time to integrate is a function of the time between two subsequent integrations Faster and cheaper integration processFaster and cheaper integration process Faster integration error recoveryFaster integration error recovery
Scaling Up Real Life ExampleReal Life Example Company SAS Institute Developers ~800 Environment Java, *Cruise control, CVS Code size ~ loc 3 CVS Branches of 5 million loc each
Unit Test Patterns
Pass/fail patterns The Simple-Test PatternThe Simple-Test Pattern The Code-Path PatternThe Code-Path Pattern The Parameter-Range PatternThe Parameter-Range Pattern
The Simple-Test Pattern Condition A Condition B Code Expected Result Expected Failure
The Code-Path Pattern Code Path A Code Path B Code Paths Path Result
The Parameter-Range Pattern Code Path A Code Path B Code Paths Path Result Success Set Failure Set
Collection management patterns The Collection-Order PatternThe Collection-Order Pattern The Enumeration PatternThe Enumeration Pattern The Collection-Constraint PatternThe Collection-Constraint Pattern The Collection-Indexing PatternThe Collection-Indexing Pattern
The Collection-Order Pattern Unordered Data Code Containing Collection Unordered Sequenced Ordered
The Enumeration Pattern Enumerator (Fwd, Rev) Code Containing Collection Edged Test Collection Expected Datum
The Collection-Constraint Pattern Null Value Collection Container Write Test Constraints Unique
The Collection-Indexing Pattern Index Key Collection Container Write Test Index Tests Out Of Bounds Index Update/Delete by Index
Data driven patterns The Simple-Test-Data PatternThe Simple-Test-Data Pattern The Data-Transformation-Test PatternThe Data-Transformation-Test Pattern
The Simple-Test-Data Pattern Input Verify Results Computation Code Output Unit Test Data Set
The Data-Transformation-Test Pattern Input Data Set Measurements Transformation Code Validation Unit Test
Data Transaction patterns The Simple-Data-I/O PatternThe Simple-Data-I/O Pattern The Constraint-Data PatternThe Constraint-Data Pattern The Rollback PatternThe Rollback Pattern
The Simple-Data-I/O Pattern Service Read Test Write Test
The Constraint-Data Pattern Nullable Service Write Test Constraints Unique Default Value Foreign Key Cascading Update Default Delete
The Rollback Pattern Service Known State Write Test rollback
Performance Patterns The Performance-Test PatternThe Performance-Test Pattern
The Performance-Test Pattern Metric At Start Code Pass Fail Metric At End Pass criteria
Simulation patterns Mock-Object PatternMock-Object Pattern The Service-Simulation PatternThe Service-Simulation Pattern The Component-Simulation PatternThe Component-Simulation Pattern
Mock-Object Pattern Abstract Methods Mock Object Implementation Real Access Layer Implementation Factory Mode Instantiates one of
The Service-Simulation Pattern Test Code Simulated Service Test I/O
The Component-Simulation Pattern Recovery Code Component Failure Test Timeout
Stress test patterns The Bulk-Data-Stress-Test PatternThe Bulk-Data-Stress-Test Pattern The Resource-Stress-Test PatternThe Resource-Stress-Test Pattern The Loading-Test PatternThe Loading-Test Pattern
The Bulk-Data-Stress-Test Pattern Data Manipulation Data Process Pass Fail Bulk Data Performance Metric
The Resource-Stress-Test Pattern Test Code Set Resource
The Loading-Test Pattern Test Code Loading
Useful links Extreme Programming Extreme Programming XP Programming Unit Test Patterns Unit Test Patterns Continuous Integration Continuous Integration ml Darrell Norton’s Blog Darrell Norton’s Blog 7.aspx
Q & A