![Page 1: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/1.jpg)
The What, Why and How of Unit Testing
Annette Allen
![Page 2: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/2.jpg)
Who is Annette Allen
DBA/Dev for 14 years Joint Chapter Leader of SQL South West based in the South
West of England Event organiser Friend of Red Gate UK Regional Mentor
![Page 3: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/3.jpg)
Agenda
Why do we test? What we test? How do we test?
![Page 4: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/4.jpg)
Why
![Page 5: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/5.jpg)
Why get requirements?
Requirements
Acceptance Criteria
Test List
![Page 6: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/6.jpg)
How do we get requirements?
Conversation Email Backlog
![Page 7: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/7.jpg)
I want . . . . .
![Page 8: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/8.jpg)
![Page 9: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/9.jpg)
Speak to stakeholder
I want. . . . . A car that is cheap, young and practical
![Page 10: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/10.jpg)
![Page 11: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/11.jpg)
![Page 12: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/12.jpg)
Requirements
I want: “A list of cars that are cheap, young and practical”
![Page 13: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/13.jpg)
Requirements
“I need a car that is under 5 years old, costs less than £10k can do more than 40mpg and can take 4 people”.
![Page 14: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/14.jpg)
Tests
1. Price is under £10k
2. Age is under 5
3. Seats >= 4
4. Fuel economy > 40 mpg
![Page 15: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/15.jpg)
Acceptance Criteria
The car must be under 5 years old The car must cost less than £10k The car must have at least 4 seats It must do at least 40 mpg
![Page 16: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/16.jpg)
What have we learned?
Ask questions Ensure no misunderstanding
Confirm requirements Measurable Quantifiable
![Page 17: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/17.jpg)
Requirements gathering
I need a report showing current, good applicants.
![Page 18: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/18.jpg)
Requirements gathering
Applicant = flag in system Current = applied for intake to start September 2016 Good = must have at least 3 predicted A Levels at AAA, AAB or
ABB
![Page 19: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/19.jpg)
Tests
Applicant = Yes Start Date > 1 September 2016 Predicted grade tests x 4:
AAA is included AAB is included ABB is included BBB is excluded
![Page 20: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/20.jpg)
Anything else?
![Page 21: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/21.jpg)
How do we test
tSQLt Community tool Licence free
Redgate SQL Test Fully licenced and supported product Part of the Developer suite
![Page 22: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/22.jpg)
Using tSQLt
Test Class All tests must start with the word ‘test’ Names should define what’s being tested Tests should test 1 thing and 1 thing only
![Page 23: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/23.jpg)
Test Object Hierarchy
GetListOfPracticalCars
GetListOfPracticalCars
Database object
Test Class
GetListOfPracticalCars.[TestAge] Test object
GetListOfPracticalCars.[TestPrice] Test object
![Page 24: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/24.jpg)
How does this work?
Fake everything Functions Data All other objects relied upon by the object tested
![Page 25: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/25.jpg)
What is Test Driven Development
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. Kent Beck, who is credited with having developed or 'rediscovered'[1] the technique, stated in 2003 that TDD encourages simple designs and inspires confidence.[2]
Taken from Wikipedia – Nov 2015
![Page 26: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/26.jpg)
Demo
![Page 27: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/27.jpg)
Downloads
http://tsqlt.org/downloads/ https://www.red-gate.com/products/sql-development/sql-test/
![Page 28: The what, why and how of Unit Testing with SQL Server](https://reader035.vdocuments.net/reader035/viewer/2022070603/586fdabc1a28ab18428b5e87/html5/thumbnails/28.jpg)