Presentation is loading. Please wait.

Presentation is loading. Please wait.

Roy Osherove Sela Group ISerializable.com (blog)

Similar presentations


Presentation on theme: "Roy Osherove Sela Group ISerializable.com (blog)"— Presentation transcript:

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

2 About Me  Agile Development  Consulting  Training  Mentoring  www.TeamAgile.com .NET  Architecture  Practices  More..

3 ArtOfUnitTesting.com

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

5 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

6 Why should I care?  Automated testing and regressions  Data Layer also contains logic!  Cost of changes is lowered

7 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

8 Integration Vs. Unit Test  Takes longer  Needs configuration  Needs large parts or whole system  Like a car engine

9 (and learn to tell the difference)

10 UI Logic Data Layer DB

11 Why NOT mock it?  You don’t test the DB logic itself  Keys  Indexes  Integrity Rules  Security  Triggers  Etc… DB

12 Unit testing against MySimpleClass

13 Rolling back DB Data  XML File (semi manual loading)  DB Restore  Transaction COM+ COM+ 1.5 System.Transactions  XtUnit  MbUnit

14 Demo

15 Resetting an identity column USE pubs GO Delete from mytable GO DBCC CHECKIDENT ('mytable', RESEED, 0) DBCC CHECKIDENT ('mytable', RESEED) GO

16 Allowing IDENTITY INSERT set IDENTITY_INSERT mytable on..insert set IDENTITY_INSERT mytable off

17 XML Loading Hard to maintain :  when there is parent-child relationships  When schema changes

18 Rolling back using COM+  Test Driven Development with Microsoft.NET  (James Newkirk)  COM+ == System.EnterpriseServices  Distributed Transactions  MyObject:ServicedComponent

19 Serviced Tests MyTestClass:ServicedComponent Transaction MySimpleClass

20

21 Serviced Tests On Code with ADO.NET Transactions MyTestClass:ServicedComponent Transaction MySimpleClass Transaction 2 SomeOtherClass

22 Problems with COM+ Rollback  What if you are calling other serviced Components?  What if they require a new transaction?

23 Serviced Tests On Serviced Components MyTestClass:ServicedComponent Transaction [Transaction( Required or supported) MySimpleClass:ServicedComponent SomeOtherClass

24

25 Serviced Tests On Serviced Components (RequiresNew) MyTestClass:ServicedComponent Transaction Transaction 2 SomeOtherClass [Transaction (RequiredNew or NotSupported) MySimpleClass:ServicedComponent

26 COM+ 1.5 MbUnit XtUnit System.Transactions

27 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

28 ServiceConfig ServiceDomain

29 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+)

30

31

32 XtUnit Pros  Decide per test on rollback behavior  Create your own attributes easily  Tools.osherove.com

33 MbUnit features  SqlRestore Info: Slow Requires exclusive DB access  Rollback Com+ 1.5

34 XUNIT.NET  http://www.codeplex.com/xunit  [AutoRollback]  Extensible

35 Types of data layers  Table Data Gateway  Row Data Gateway  Active Record  Data Mapper  Book: “Patterns of Enterprise Application Architecture” Martin Fowler

36 Tests Against NHibernate Based Apps MyTestClass Transaction MyClass NHibernate

37 Tests Against ActiveRecord MyTestClass Transaction MyActiveCategoryClass NHibernate

38 Demo

39 Summary  DB Integration tests are necessary  Try combination of DB tests Application-integration tests  DataDude is your friend!  Use frameworks System.Transactions XtUnit MbUnit

40 A song by Roy Osherove (+ Simon & Garfunkel)

41

42

43

44

45

46

47

48

49

50

51

52

53

54 Roy@Osherove.com ISerializable.com (blog)

55 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


Download ppt "Roy Osherove Sela Group ISerializable.com (blog)"

Similar presentations


Ads by Google