44
Unit Tests Unit Tests Руслан Трифонов Руслан Трифонов Omegasoft Ltd. Omegasoft Ltd.

Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Embed Size (px)

Citation preview

Page 1: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Unit TestsUnit TestsРуслан ТрифоновРуслан Трифонов

Omegasoft Ltd.Omegasoft Ltd.

Page 2: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

СъдържаниеСъдържание

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. РесурсиРесурси

Page 3: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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.

Page 4: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Scope

Design

Development

Test and FixTime flow

Planning

Unit TestingSystem Testing

Integration TestingFunctional TestingRelease Testing

Unit Tests and Application Unit Tests and Application LifecycleLifecycle

Page 5: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

• 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

Page 6: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

• 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

Page 7: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

• Code and TestCode and Test

• Test FirstTest First

Unit Testing ApproachUnit Testing Approach

Page 8: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Write code

Write unit test

Run and succeed

Code and TestCode and Test

Time flow

Page 9: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 10: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 11: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

• 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?

Page 12: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 13: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

DevelopmentWorkstation Source Code

Repository Server

Integration Server

Publish Results

Continuous IntegrationContinuous Integration

•Check out•Build•Perform BVT•Publish results

Check-in

Page 14: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 15: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Scaling UpScaling Up

• Real Life ExampleReal Life Example

CompanyCompany SAS InstituteSAS Institute

DevelopersDevelopers ~~800800

EnvironmentEnvironment Java, *Cruise control, CVSJava, *Cruise control, CVS

Code 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

Page 16: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Unit Test PatternsUnit Test Patterns

Page 17: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management 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

Page 18: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Simple-Test PatternThe Simple-Test Pattern

Condition A

Condition BCode

Expected Result

Expected Failure

Page 19: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Code-Path PatternThe Code-Path Pattern

Code Path A

Code Path BCode Paths

Path Result

Path Result

Page 20: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Parameter-Range PatternThe Parameter-Range Pattern

Code Path A

Code Path BCode Paths

Path Result

Path Result

Success Set

Failure Set

Page 21: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 22: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Collection-Order PatternThe Collection-Order Pattern

Unordered DataCode

Containing Collection

Unordered

Sequenced

Ordered

Page 23: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Enumeration PatternThe Enumeration Pattern

Enumerator(Fwd, Rev)

Code Containing Collection

Edged Test

Collection

Expected Datum

Page 24: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Collection-Constraint PatternThe Collection-Constraint Pattern

Null Value

CollectionContainer

Write Test

Constraints

Unique

Page 25: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Collection-Indexing Pattern The Collection-Indexing Pattern

Index Key

CollectionContainer

Write Test

Index Tests

Out Of Bounds Index

Update/Delete by Index

Page 26: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 27: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Simple-Test-Data PatternThe Simple-Test-Data Pattern

Input

Verify ResultsComputation Code

Output

Unit Test

Data Set

Page 28: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Data-Transformation-Test PatternThe Data-Transformation-Test Pattern

Input Data Set

Measurements

Transformation Code

ValidationUnit Test

Page 29: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 30: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Simple-Data-I/O PatternThe Simple-Data-I/O Pattern

Service Read TestWrite Test

Page 31: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Constraint-Data PatternThe Constraint-Data Pattern

Nullable

ServiceWrite Test

Constraints

Unique

Default Value

Foreign Key

Cascading Update

Default Delete

Page 32: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Rollback PatternThe Rollback Pattern

Service

Known State

Write Test

rollback

Page 33: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Performance PatternsPerformance Patterns

• The Performance-Test PatternThe Performance-Test Pattern

Page 34: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Performance-Test PatternThe Performance-Test Pattern

Metric At Start Code

Pass

Fail

Metric At End

Pass criteria

Page 35: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 36: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Mock-Object PatternMock-Object Pattern

Abstract Methods

Mock Object

Implementation

Real Access Layer

Implementation

FactoryMode

Instantiates one of

Page 37: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Service-Simulation PatternThe Service-Simulation Pattern

Test Code

Simulated Service

Test

I/O

Page 38: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Component-Simulation PatternThe Component-Simulation Pattern

Recovery Code

Component Failure

Test

Timeout

Page 39: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 40: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Bulk-Data-Stress-Test PatternThe Bulk-Data-Stress-Test Pattern

Data Manipulation

Data Process

Pass

Fail

Bulk Data

Performance Metric

Page 41: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Resource-Stress-Test PatternThe Resource-Stress-Test Pattern

Test CodeSet Resource

Page 42: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

The Loading-Test PatternThe Loading-Test Pattern

Test CodeLoading

Page 43: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

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

Page 44: Unit Tests Руслан Трифонов Omegasoft Ltd.. Съдържание 1.Въведение 2.Unit test patterns 2.1. Pass/fail patterns 2.2. Collection management patterns

Q & AQ & A

Q & AQ & A