440 likes | 752 Views
Unit Tests. Руслан Трифонов Omegasoft Ltd. Съдържание. Въведение 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
E N D
Unit Tests Руслан Трифонов Omegasoft Ltd.
Съдържание • Въведение • 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 • Ресурси
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.
Unit Tests and Application Lifecycle Scope Planning System Testing Integration Testing Functional Testing Release Testing Design Unit Testing Development Test and Fix Time flow
Some facts • Unit test framework is needed. • Unit tests are written by developers for developers • Unit tests are released into the code repository along with the code they test
More facts • Trivial code may be omitted (property getters and setters) • Ideally all unit tests should pass before check-in
Unit Testing Approach • Code and Test • Test First
Code and Test Write code Write unit test Run and succeed Time flow
Test Driven Development Create a testlist Pick а test Write test Compile and Fail Write enough code to compile Run test and fail Write code to pass test Remove duplication Time flow
Why TDD? • Helps find design issues early and avoidsrework • Writing code to satisfy a test is a focusedactivity –less chance of error • Tests will be a more comprehensive thanwhen written after code
Why Unit Tests? • Unit tests improve design • Unit tests are good documentation • Unit tests enable collective code ownership
Why Unit Tests? • Unit tests decrease the defect-injection rate due to refactoring / changes • Unit tests decrease the cost of the changes • Unit tests bring higher development velocity in the latest dev. stages • Unit tests enable frequent integration
Continuous Integration • Check out • Build • Perform BVT • Publish results Check-in Development Workstation SourceCode Repository Server Integration Server Publish Results
Why Continuous Integration? The time to integrate is a function of the time between two subsequentintegrations • Faster and cheaper integration process • Faster integration error recovery
Scaling Up • Real Life Example
Pass/fail patterns • The Simple-Test Pattern • The Code-Path Pattern • The Parameter-Range Pattern
Code Expected Result Condition A Expected Failure Condition B The Simple-Test Pattern
The Code-Path Pattern Code Paths Path Result Code Path A Path Result Code Path B
Success Set Code Paths Path Result Code Path A Path Result Code Path B Failure Set The Parameter-Range Pattern
Collection management patterns • The Collection-Order Pattern • The Enumeration Pattern • The Collection-Constraint Pattern • The Collection-Indexing Pattern
The Collection-Order Pattern Unordered Code Containing Collection Sequenced Unordered Data Ordered
The Enumeration Pattern Code Containing Collection Edged Test Expected Datum Enumerator (Fwd, Rev) Collection
The Collection-Constraint Pattern Collection Container Write Test Null Value Unique Constraints
The Collection-Indexing Pattern Collection Container Write Test Index Key Out Of Bounds Index Update/Delete by Index Index Tests
Data driven patterns • The Simple-Test-Data Pattern • The Data-Transformation-Test Pattern
The Simple-Test-Data Pattern Data Set Input Output Computation Code Verify Results Unit Test
The Data-Transformation-Test Pattern Input Data Set Validation Transformation Code Unit Test Measurements
Data Transaction patterns • The Simple-Data-I/O Pattern • The Constraint-Data Pattern • The Rollback Pattern
The Simple-Data-I/O Pattern Read Test Service Write Test
The Constraint-Data Pattern Service Write Test Nullable Unique Default Value Foreign Key Cascading Update Default Delete Constraints
The Rollback Pattern rollback Service Write Test Known State
Performance Patterns • The Performance-Test Pattern
The Performance-Test Pattern Pass Code Metric At Start Metric At End Pass criteria Fail
Simulation patterns • Mock-Object Pattern • The Service-Simulation Pattern • The Component-Simulation Pattern
Mock-Object Pattern Abstract Methods Mock Object Implementation Real Access Layer Implementation Instantiates one of Factory Mode
The Service-Simulation Pattern Simulated Service I/O Test Test Code
The Component-Simulation Pattern Component Failure Timeout Recovery Code Test
Stress test patterns • The Bulk-Data-Stress-Test Pattern • The Resource-Stress-Test Pattern • The Loading-Test Pattern
The Bulk-Data-Stress-Test Pattern Pass Bulk Data Performance Metric Data Process Data Manipulation Fail
The Resource-Stress-Test Pattern Code Test Set Resource
The Loading-Test Pattern Code Test Loading
Useful links Extreme Programmingwww.extremeprogramming.org XP Programmingwww.xprogramming.com Unit Test Patternswww.codeproject.com/gen/design/autp5.asp Continuous Integration www.martinfowler.com/articles/continuousIntegration.html Darrell Norton’s Blogwww.codebetter.com/blogs/darrell.norton/articles/50337.aspx
Q & A Q & A