You are on page 1of 23

SQLServerUnitTesting

JoelChampagne
President,CodeX President CodeX Enterprises

July2011
MarkGinnebaugh,UserGroupLeader, mark@designmind.com mark@designmind com

WhoamI? Who am I?
President CodeX Enterprises LLC PresidentCodeX EnterprisesLLC 20yearsconsultingexperience
L Largemissioncriticalsystems i i iti l t .NET,SQL20002008,SSAS,SSRS,SSIS Architecture,DataArchitecture

Focusondeveloperproductivitytools

Why?
UnitTestingwhatsaunit? U it t ti h Unittestinghasprovenvaluableinmany l bl i languages Things can break for countless reasons find Thingscanbreakforcountlessreasons find thosebreakagesearly SomemaysaySQLhasgottentheshortendof thestickhereforvariousreasons h i kh f i
Harder:time/efforttowritetestsforpossibly thousandsofobjects(manyofwhichcouldbeCRUD) j ( y ) Separationofdevelopmentprocess Statefulness:dependenceonspecificdatatomakeit fly(well) fly (well)

ScenariosforFailure Scenarios for Failure


Supposetheresanexistingprocedurewhich hasbeenworkingforages Someone renames a table column, missed Someonerenamesatablecolumn,missed updatingthisprocedureforwhateverreason (e.g.differentdatabase) (e.g. different database) Failsatruntimeyouwouldhaveknown, however,ifithadatleastbeen exercised however if it had at least been exercised Also,whatdoesSSMStelluswhenwecommit thebrokenprocedure? th b k d ?

IntegrationPoints Integration Points


On Demand OnDemand Scheduled S Strategic i
Build/movingobjects

Automation
Includesonchange ContinuousIntegrationstrategies

VisualStudio2010 Visual Studio 2010


AbilitytoruntestswithinVS Ability to run tests within VS Setuptests,perobject Canhaveexpectedresults C h d l SchemaViewofferseasywaytoscript,per object Demo

Observations
Quitepowerfulaswecancraftdatadriven testsusingabitofC# tests using a bit of C#
NotnecessarilytestingSQLdirectly(canbe.NET components) p ) Nextslideexample

Hoststestsinrunnablecode(SQLscripts ( Q p themselvesstoredinresourcefile) Integratesaspartofalargertesting g p g g infrastructureavailablewithVisualStudio


Testrigs,agents,etc. Canmakeascomplexasdesired

Datadrivenexample

SQLHero
Problemstryingtoaddress:
Developersoftendontliketoinvesttimeinwritingtests
At least get some touch testsasastartingpoint Atleastgetsome touchtests as a starting point

Wanttoexercisetestsearlyandoften!
OnALTERandscheduled(andondemand)

L Leveragetestswedohaveatstrategictimes:e.g.scripting d h i i i i changestoanewplace
Canincludetheseautomaticallyfromtheschemacomparetool!

Settingoftargets,notjustthresholds(sowecando reporting/analysisvs.targets) Tests can be somewhat portable(i.e.donotneedto Testscanbesomewhat portable (i.e. do not need to strictlytietoasingledatabase) Realityofdatabaseswithhundredsoftables,reference data,etc. maynotbepracticaltoregentheentirething data etc may not be practical to re gen the entire thing fromscripts,etc.

Configuration g

TestingPolicies.xml
Example:aproc parameterthatendsinAreaID,populateusingaselectfromAreatablethatisfiltered <PARAMETER> <PATTERN>AreaID$</PATTERN> <SQL_LOOKUP>SELECTAreaID FROMAreaaWHEREa.AreaAbbr LIKE'A%'</SQL_LOOKUP> / </PARAMETER> Example:aproc parameterof@DocTypeID thatrelatestoatablecalledCommDocumentType <PARAMETER> PARAMETER <NAME>@DocTypeID</NAME> <CHANGE_NAME_TO>CommDocumentType</CHANGE_NAME_TO> </PARAMETER> Example:asampleplugin,putintheprogramfiles\sqlhero\pluginsdirectory <PLUGIN> <ASSEMBLY_FILE>SampleTestingPlugIn.dll</ASSEMBLY_FILE> <TYPENAME>SampleTestingPlugIn.MyTestPolicy</TYPENAME> </PLUGIN> Example:allobjectsintheDebugschemawouldbeexcludedfromtesting <OBJECT> <PATTERN>^\[?Debug\]?\.</PATTERN> <IS_EXCLUDED>True</IS_EXCLUDED> </OBJECT>

Notes
The test generation process will look at Thetestgenerationprocesswilllookat capturedworkloadstoseeifanalready executedinvocation willwork executed invocation will work Isusingarandomizedprocess,butinfluenced bytestingpolicyfile by testing policy file Nosideeffects
Rollbackperobject Advantages/Disadvantages

CreatingTests g

RunningTests Running Tests


Scheduling ViaTemplate ViaTemplate,withparallelexecution! i l ih ll l i ! FromSHCommand.exe From.sqlheroproj VisualStudioprojecttype Demo

SomeObservations
Presenceoftouchtestinghasprovenvaluefrom experience experience example SQLHerotestingcanaugmentVStesting:
VSforformalunittestswhicharepartofbuild process,complexcaseswherecanuseC# SHfortouchteststhatarejusttherebehindthe scenestofindcertainissuesforyoutransparently y p y OutputofSHtestingtemplatecanbeusedinVS,if youlike!

Not intending SQL Hero to do it allbut NotintendingSQLHeroto doitall but consideringsomenewfeaturesalongthelinesof turningcapturedworkloadsintocomplextest cases(C#) ( )

SomeObservations
Presenceoftouchtestinghasprovenvaluefrom experience experience example SQLHerotestingcanaugmentVStesting:
VSforformalunittestswhicharepartofbuild process,complexcaseswherecanuseC# SHfortouchteststhatarejusttherebehindthe scenestofindcertainissuesforyoutransparently y p y OutputofSHtestingtemplatecanbeusedinVS,if youlike!

Not intending SQL Hero to do it allbut NotintendingSQLHeroto doitall but consideringsomenewfeaturesalongthelinesof turningcapturedworkloadsintocomplextest cases(C#) ( )

Amorecomplexusecase p
Problemstryingtoaddress:
Dataqualityindevelopmentregions Securityandisolationofproductiondata Inprogresswork(usingexternalsourcecontrol?) Makingthiseasy Making this easy

Howdoesunittestingrelate?
Integratedaspartoftheprocesstoensurethingsstill workattheend(couldbeadifferentprocesstoo) work at the end (could be a different process too) Overallprocesscanserveasonewaytoachievethe covetedknownstartingpoint easily

YMMV maybesimplerormorecomplex(likely b i l l (lik l supported!) p p g g Watchforawhitepaperdescribingthisingreatdetail Demo

Amorecomplexusecase illustrated
Production QA/Development

Prod (remote)

QANew Backup

QA

Backup/ Restore Backup

QANew (remote)
Scramble sensitive data

Remove

Amorecomplexusecase illustrated
Production QA/Development

Prod (remote)

QANew Backup

Detach,Move Detach, Move Files,Attach

QA

Sync Pending Changes

QANew Backup
Restore

FTPLanding Zone

QANew

Performtests

Amorecomplexusecase illustrated
Production QA/Development

Prod (remote)

QAOld

Email relevant partiesabout parties about completion

QA

Puttingittogether
VS Database Project VSDatabaseProject
Build>Deploy(localorshared) Canexercisestaticcodeanalysis Canincludeunittesting Can include unit testing CanuseMSBuild,whichinturnintegrateswithTFS/CIoptions

SQLHero
Precludesnoneoftheabove Precludes none of the above Addstouchtestingcapabilityduringdevelopmentcycle,transparently Sourcecontrolintegrationinterops withDatabaseProjects! Largedatasetsareencouragedandeasytobuild Compliancecheckingincorporatedthereactsagainstrealdatabases,can bescheduled,emaildelivery(advantages!) Supportsautomatedoperations
Example: DB reconcile from branched dev to branched QA (both stateful worked Example:DBreconcilefrombrancheddev tobranchedQA(bothstateful,worked prettyflawlesslywithasimple.cmd script)

Moreusecasestosupport:testingfromacollectedworkload

Yourenvironment
Dodevelopersdoactualdatamodeling? Dodeveloperswantasharedenvironment?

Offers
LookingforCustomerAdvisoryBoard members
Freeenterpriseeditioninexchangeforongoing feedback(iterativedevelopmentonv next feedback (iterative development on vnext features)

Nextreleasecomingsoon(alongwithnew g ( g whitepapers)
Stillfree! www.codexframework.com (Library) @sqlheroguy joelc@codexframework.com

Tolearnmoreorinquireaboutspeakingopportunities,pleasecontact: o ea o e o qu e about spea g oppo tu t es, p ease co tact: MarkGinnebaugh,UserGroupLeader mark@designmind.com

You might also like