Upload
darci
View
132
Download
0
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Unit TestsUnit TestsРуслан ТрифоновРуслан ТрифоновOmegasoft Ltd.Omegasoft Ltd.
СъдържаниеСъдържание
1.1. ВъведениеВъведение2.2. Unit test patternsUnit test patterns
2.1. Pass/fail patterns 2.1. Pass/fail patterns
2.2. Collection management patterns 2.2. Collection management patterns
2.3. Data driven patterns 2.3. Data driven patterns
2.4. Data transaction patterns2.4. Data transaction patterns
2.5. Performance patterns 2.5. Performance patterns
2.6. Process patterns 2.6. Process patterns
2.7. Simulation patterns 2.7. Simulation patterns
2.8. Stress test patterns2.8. Stress test patterns
3.3. РесурсиРесурси
DefinitionDefinition
A A unit test unit test is a piece of code written by a is a piece of code written by a developer that exercises a very small, specific developer that exercises a very small, specific area of functionality of the code being tested.area of functionality of the code being tested.
Scope
Design
Development
Test and FixTime flow
Planning
Unit TestingSystem Testing
Integration TestingFunctional TestingRelease Testing
Unit Tests and Application Unit Tests and Application LifecycleLifecycle
• Unit test framework is needed.Unit test framework is needed.
• Unit tests are written by developers for Unit tests are written by developers for developersdevelopers
• Unit tests are released into the code Unit tests are released into the code repository along with the code they test repository along with the code they test
Some factsSome facts
• Trivial code may be omitted (property getters Trivial code may be omitted (property getters and setters)and setters)
• Ideally all unit tests should pass before check-inIdeally all unit tests should pass before check-in
More factsMore facts
• Code and TestCode and Test
• Test FirstTest First
Unit Testing ApproachUnit Testing Approach
Write code
Write unit test
Run and succeed
Code and TestCode 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 DevelopmentTest Driven Development
Time flow
Write test
Remove duplication
Why TDD?Why TDD?
• Helps find design issues early and avoidsHelps find design issues early and avoids reworkrework
• Writing code to satisfy a test is a focusedWriting code to satisfy a test is a focused activity –activity –less chance of errorless chance of error
• Tests will be a more comprehensive thanTests will be a more comprehensive than when when written after codewritten after code
• Unit tests improve design Unit 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?Why Unit Tests?
Why Unit Tests?Why Unit Tests?
• Unit tests decrease the defect-injection rate due Unit tests decrease the defect-injection rate due to refactoring / changesto refactoring / changes
• Unit tests decrease the cost of the changes Unit tests decrease the cost of the changes
• Unit tests bring higher development velocity in Unit tests bring higher development velocity in the latest dev. stagesthe latest dev. stages
• Unit tests enable frequent integrationUnit tests enable frequent integration
DevelopmentWorkstation Source Code
Repository Server
Integration Server
Publish Results
Continuous IntegrationContinuous Integration
•Check out•Build•Perform BVT•Publish results
Check-in
Why Continuous Integration?Why Continuous Integration?
The time to integrate is a function of the time The time to integrate is a function of the time between two subsequentbetween two subsequent integrations integrations
• Faster and cheaper integration processFaster and cheaper integration process
• Faster integration error recoveryFaster integration error recovery
Scaling UpScaling Up
• Real Life ExampleReal Life Example
CompanyCompany SAS InstituteSAS InstituteDevelopersDevelopers ~~800800EnvironmentEnvironment Java, *Cruise control, CVSJava, *Cruise control, CVSCode sizeCode size ~15 000 000 loc~15 000 000 loc
3 CVS Branches of 5 million loc each3 CVS Branches of 5 million loc each
Unit Test PatternsUnit Test Patterns
Pass/fail patternsPass/fail patterns
• The Simple-Test Pattern The Simple-Test Pattern
• The Code-Path Pattern The Code-Path Pattern
• The Parameter-Range Pattern The Parameter-Range Pattern
The Simple-Test PatternThe Simple-Test Pattern
Condition A
Condition BCode
Expected Result
Expected Failure
The Code-Path PatternThe Code-Path Pattern
Code Path A
Code Path BCode Paths
Path Result
Path Result
The Parameter-Range PatternThe Parameter-Range Pattern
Code Path A
Code Path BCode Paths
Path Result
Path Result
Success Set
Failure Set
Collection management patternsCollection management patterns
• The Collection-Order Pattern The Collection-Order Pattern • The Enumeration Pattern The Enumeration Pattern • The Collection-Constraint Pattern The Collection-Constraint Pattern • The Collection-Indexing PatternThe Collection-Indexing Pattern
The Collection-Order PatternThe Collection-Order Pattern
Unordered DataCode
Containing Collection
Unordered
Sequenced
Ordered
The Enumeration PatternThe Enumeration Pattern
Enumerator(Fwd, Rev)
Code Containing Collection
Edged Test
Collection
Expected Datum
The Collection-Constraint PatternThe Collection-Constraint Pattern
Null Value
CollectionContainer
Write Test
Constraints
Unique
The Collection-Indexing Pattern The Collection-Indexing Pattern
Index Key
CollectionContainer
Write Test
Index Tests
Out Of Bounds Index
Update/Delete by Index
Data driven patternsData driven patterns
• The Simple-Test-Data Pattern The Simple-Test-Data Pattern
• The Data-Transformation-Test Pattern The Data-Transformation-Test Pattern
The Simple-Test-Data PatternThe Simple-Test-Data Pattern
Input
Verify ResultsComputation Code
Output
Unit Test
Data Set
The Data-Transformation-Test PatternThe Data-Transformation-Test Pattern
Input Data Set
MeasurementsTransformation
Code
ValidationUnit Test
Data Transaction patternsData Transaction patterns
• The Simple-Data-I/O Pattern The Simple-Data-I/O Pattern
• The Constraint-Data Pattern The Constraint-Data Pattern
• The Rollback Pattern The Rollback Pattern
The Simple-Data-I/O PatternThe Simple-Data-I/O Pattern
Service Read TestWrite Test
The Constraint-Data PatternThe Constraint-Data Pattern
Nullable
ServiceWrite Test
Constraints
Unique
Default Value
Foreign KeyCascading Update
Default Delete
The Rollback PatternThe Rollback Pattern
Service
Known State
Write Test
rollback
Performance PatternsPerformance Patterns
• The Performance-Test PatternThe Performance-Test Pattern
The Performance-Test PatternThe Performance-Test Pattern
Metric At Start Code
Pass
Fail
Metric At End
Pass criteria
Simulation patternsSimulation patterns
• Mock-Object Pattern Mock-Object Pattern
• The Service-Simulation Pattern The Service-Simulation Pattern
• The Component-Simulation Pattern The Component-Simulation Pattern
Mock-Object PatternMock-Object Pattern
Abstract Methods
Mock Object
Implementation
Real Access Layer
Implementation
FactoryMode
Instantiates one of
The Service-Simulation PatternThe Service-Simulation Pattern
Test Code
Simulated Service
Test
I/O
The Component-Simulation PatternThe Component-Simulation Pattern
Recovery Code
Component Failure
Test
Timeout
Stress test patternsStress test patterns
• The Bulk-Data-Stress-Test Pattern The Bulk-Data-Stress-Test Pattern
• The Resource-Stress-Test Pattern The Resource-Stress-Test Pattern
• The Loading-Test Pattern The Loading-Test Pattern
The Bulk-Data-Stress-Test PatternThe Bulk-Data-Stress-Test Pattern
Data Manipulation
Data Process
Pass
Fail
Bulk Data
Performance Metric
The Resource-Stress-Test PatternThe Resource-Stress-Test Pattern
Test CodeSet Resource
The Loading-Test PatternThe Loading-Test Pattern
Test CodeLoading
Useful linksUseful links
Extreme ProgrammingExtreme Programmingwww.extremeprogramming.org
XP Programmingwww.xprogramming.com
Unit Test PatternsUnit Test Patternswww.codeproject.com/gen/design/autp5.asp
Continuous Integration Continuous Integration www.martinfowler.com/articles/continuousIntegration.html
Darrell Norton’s BlogDarrell Norton’s Blogwww.codebetter.com/blogs/darrell.norton/articles/50337.aspx
Q & AQ & A
Q & AQ & A