Roy Osherove Sela Group [email protected] ISerializable.com (blog)

55
Roy Osherove Sela Group [email protected] ISerializable.com (blog)

Transcript of Roy Osherove Sela Group [email protected] ISerializable.com (blog)

Page 1: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Roy OsheroveSela [email protected]

ISerializable.com (blog)

Page 2: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

About Me

Agile Development

Consulting Training Mentoring

www.TeamAgile.com

.NET

Architecture Practices More..

Page 3: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

ArtOfUnitTesting.com

Page 4: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Agenda

Unit Testing Vs. Integration Testing To mock or not to mock the DB? Rolling back DB state

Restore, Manual load, COM+, XTUnit, System.Transactions

Working and testing with DataSets Pure DB tests with DataDude Short song?

Page 5: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Demos

Rollbacks: COM+ 1.0 COM+ 1.5

(ServiceConfig) XtUnit MbUnit [DataRollback] MbUnit [SqlDbRestore] TransactionScope

Comparing DataSets DataDude

DataSet loader with XML file update and delete

NHibernate ORM ActiveRecord testing

Page 6: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Why should I care?

Automated testing and regressions Data Layer also contains logic! Cost of changes is lowered

Page 7: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

What is a Unit-Testable System

For each piece of coded logic in the system, a unit test can be written easily enough to verify it works as expected while keeping the PC-COF rules

Partial runs are possible Configuration is not needed Consistent pass/fail result Order does not matter Fast

Page 8: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Integration Vs. Unit Test

Takes longer Needs configuration Needs large parts or whole system

Like a car engine

Page 9: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

(and learn to tell the difference)

Page 10: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

UI

Logic

Data LayerDB

Page 11: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Why NOT mock it? You don’t test the DB

logic itself

Keys Indexes Integrity Rules Security Triggers Etc…

DB

Page 12: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Unit testing against MySimpleClass

Page 13: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Rolling back DB Data

XML File (semi manual loading) DB Restore Transaction

COM+COM+ 1.5System.Transactions

XtUnit MbUnit

Page 14: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Demo

Page 15: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Resetting an identity column

USE pubs GO

Delete from mytableGO

DBCC CHECKIDENT ('mytable', RESEED, 0) DBCC CHECKIDENT ('mytable', RESEED)

GO

Page 16: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Allowing IDENTITY INSERT

set IDENTITY_INSERT mytable on

..insert

set IDENTITY_INSERT mytable off

Page 17: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

XML Loading

Hard to maintain : when there is parent-child

relationships When schema changes

Page 18: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Rolling back using COM+

Test Driven Development with Microsoft.NET

(James Newkirk)

COM+ == System.EnterpriseServices Distributed Transactions

MyObject:ServicedComponent

Page 19: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Serviced Tests

MyTestClass:ServicedComponent

Transaction

MySimpleClass

Page 20: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 21: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Serviced Tests On Code with ADO.NET Transactions

MyTestClass:ServicedComponent

Transaction

MySimpleClass

Transaction 2

SomeOtherClass

Page 22: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Problems with COM+ Rollback

What if you are calling other serviced Components?

What if they require a new transaction?

Page 23: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Serviced Tests On Serviced Components

MyTestClass:ServicedComponent

Transaction

[Transaction( Required or supported)

MySimpleClass:ServicedComponent

SomeOtherClass

Page 24: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 25: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Serviced Tests On Serviced Components (RequiresNew)

MyTestClass:ServicedComponent

Transaction

Transaction 2

SomeOtherClass

[Transaction (RequiredNew or NotSupported)

MySimpleClass:ServicedComponent

Page 26: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

COM+ 1.5MbUnitXtUnitSystem.Transactions

Page 27: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

COM+ 1.0 Vs. 1.5

1.5 requires Win XP SP 2 or higher WinServer 2003 SP1 or higher

Otherwise – you have to use 1.0

Page 28: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

ServiceConfig

ServiceDomain

Page 29: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

System.Transactions (.NET 2.0)

Using(TransactionScope scope = new TransactionScope)

{

…do DB work here

}

Automatic promotion of transactions Very simple syntax

None ADO.NET Transaction Distributed Transaction (COM+)

Page 30: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 31: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 32: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

XtUnit Pros

Decide per test on rollback behavior Create your own attributes easily Tools.osherove.com

Page 33: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

MbUnit features

SqlRestore Info:SlowRequires exclusive DB access

RollbackCom+ 1.5

Page 34: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

XUNIT.NET

http://www.codeplex.com/xunit

[AutoRollback] Extensible

Page 35: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Types of data layers

Table Data Gateway Row Data Gateway Active Record Data Mapper

Book: “Patterns of Enterprise Application Architecture”Martin Fowler

Page 36: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Tests Against NHibernate Based Apps

MyTestClass

Transaction

MyClass

NHibernate

Page 37: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Tests Against ActiveRecord

MyTestClass

Transaction

MyActiveCategoryClass

NHibernate

Page 38: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Demo

Page 39: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Summary

DB Integration tests are necessary Try combination of

DB testsApplication-integration tests

DataDude is your friend! Use frameworks

System.TransactionsXtUnitMbUnit

Page 40: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

A song by Roy Osherove

(+ Simon & Garfunkel)

Page 41: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 42: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 43: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 44: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 45: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 46: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 47: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 48: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 49: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 50: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 51: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 52: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 53: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)
Page 54: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

[email protected]

ISerializable.com (blog)

Page 55: Roy Osherove Sela Group Roy@Osherove.com ISerializable.com (blog)

Resources

PoEAA Book NHibernate ActiveRecord XtUnit MbUnit NUnit DataDude

Database Unit Testing article

DataSet compare The Art of Unit Testing

Book Roy’s Blog:

ISerializable.com