get testing with tsqlt - sql in the city workshop 2014

35
Get testing with tSQLt Practical examples and automation Steve Jones Red Gate Software Part III of the Continuous Delivery for Databases series

Upload: red-gate-software

Post on 19-Jun-2015

218 views

Category:

Software


0 download

DESCRIPTION

Get Testing with tSQLt. Practical Examples and automation. Steve Jones presentation from SQL In The City.

TRANSCRIPT

  • 1. Get testing with tSQLtPractical examples and automationPart III of the Continuous Delivery for Databases seriesSteve JonesRed Gate Software

2. Agenda Goals Who am I? The Delivery Pipeline Where do I test? Why do I test? tSQLt Refactoring anapplication Testing proceduresand functions Grouping tests Testing exceptions 3. Get in touchSteve [email protected]@way0utwest 4. Goals Why testing matters Introduction to the tSQLt framework What and how we can test Testing as part of a deployment pipeline 5. Database release pipelineContinuousIntegrationDevelopment Source Control Testing and review Deployment 6. Database release pipelineContinuousIntegrationDevelopment Source Control Testing and review DeploymentIn this presentation 7. Continuous integration an overviewBuild TestPublish SyncAutomatically runby CI build serverSourcecontrolReleasemanagementTrigger 8. Firstthe big questionWhen do I test? 9. Different types of testFor different stages of the pipelineDevelopmentIntegrationTestingQAPre-Production/StagingProductionUnit testsIntegration testsAcceptance testsDeployment validationBehaviour validationPerformance testsOther validations 10. Why test? 11. Fix bugs as soon as you find them Unfixed bugs camouflageother bugs Unfixed bugs suggest qualityisnt important Discussing unfixed bugs is awaste of time Unfixed bugs lead toduplicate effort Unfixed bugs lead tounreliable metrics Unfixed bugs distract theentire team Unfixed bugs hinder short-noticereleases Unfixed bugs lead toinaccurate estimates Fixing familiar code is easierthan unfamiliar code Fixing a bug today costs lessthan tomorrow 12. The next question:What do I test? 13. How is data retrieved, stored andmaintained in your application?[SQL code] includes views, stored procedures, functions, triggers,the creation of tables and the relationships between them, andquery statements embedded in other programming languages.Writing this code often involves decisions about thenature of data being processed, complicated joiningand filtering, performance tuning, data cleansing,replication and data maintenance.Source: Sebastian Meine and Dennis Lloyd SQL Server Unit Testing with tSQLt simple-talk.com 14. What do we test? Calculations in procedures and functions Constraints Edge cases of data DML Expected behavior of data DML Error Handling Standards 15. DemoStandards Ensure that the standards youcare about are followed. SQLCop sqlcop.lessthandot.com Easy to write your own 16. Why tSQLt? Free framework, similar to nUnit/jUnit Tests written in T-SQL Can use SSMS IDE Run tests singly, in groups and in any order Self-contained tests isolated transaction Includes common assertions to reduce repetitive coding Requires the SQLCLR 17. Structure of tests Classes Group by object/area being tested Layout Assemble Act Assert Tests fail first 18. Our user storyReader on our Simple-Talk website:I want to be able to see how longit will take me to read an article. 19. Our tasksWe need to refactor and adjust the reading time for articlesTo do Doing DoneProc toupdatereadingtimeTest anedgecaseGrouptests andexecuteAdd acolumn toour table 20. DemoTesting the API Our first test tsqlt.AssertResultSetsHaveSameMetaData 21. Our tasksWe need to refactor and adjust the reading time for articlesTo do Doing DoneProc toupdatereadingtimeTest anedgecaseGrouptests andexecuteAdd acolumn toour table 22. DemoProcedures and functions Test the function Create a new procedure Isolate the procedure fromthe function in a test 23. Our tasksWe need to refactor and adjust the reading time for articlesTo do Doing DoneProc toupdatereadingtimeTest anedgecaseGrouptests andexecuteAdd acolumn toour table 24. Exceptions Our code needs good error handling We want to test for this by: Creating errors with edge cases Testing for specific exceptions when we use bad data 25. DemoExceptions Send in bad data in a test Update our procedure witherror handling Include a new test to catchthe exception 26. Exceptions NULL -x 0 Long strings (esp char) Nvarchar v varchar Int/char v datetime Formatting (mm/dd/yyyy v. dd/mm/yyyy) 27. Our tasksWe need to refactor and adjust the reading time for articlesTo do Doing DoneProc toupdatereadingtimeTest anedgecaseGrouptests andexecuteAdd acolumn toour table 28. Building a Test Suite By continuing to grow your test suite with each change,developers spread the load By having a large suite, we have better code coverage We can easily regression test However, we need to group tests 29. Our tasksWe need to refactor and adjust the reading time for articlesTo do Doing DoneProc toupdatereadingtimeTest anedgecaseGrouptests andexecuteAdd acolumn toour table 30. Write one extra test 31. Goals Why testing matters and what it contributes Introduction to the tSQLt framework What and how we can test - examples Testing as part of a deployment pipeline 32. THE ENDAny questions?www.sqlservercental.comwww.simple-talk.comPatterns and Practices [email protected]@way0utwest 33. References Continuous Integration http://en.wikipedia.org/wiki/Continuous_integration Getting Started Testing Databases with tSQLt -https://www.simple-talk.com/sql/t-sql-programming/getting-started-testing-databases-with-tsqlt/ 34. Image sourcesAuthor Source InformationPeter Kaminski Flickr Safe Area Flickr. This file is licensed under the Creative Commons Attribution 2.0 Generic license.Harold Groven Flickr Fuse box Flickr. This file is licensed under the Creative Commons Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0)license.William Warby Flickr 1up - Flickr. This file is licensed under the Creative Commons Attribution 2.0 Generic license. 35. Get in touchSteve [email protected]@way0utwest