the what, why and how of unit testing with sql server
TRANSCRIPT
The What, Why and How of Unit Testing
Annette Allen
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
Agenda
Why do we test? What we test? How do we test?
Why
Why get requirements?
Requirements
Acceptance Criteria
Test List
How do we get requirements?
Conversation Email Backlog
I want . . . . .
Speak to stakeholder
I want. . . . . A car that is cheap, young and practical
Requirements
I want: “A list of cars that are cheap, young and practical”
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”.
Tests
1. Price is under £10k
2. Age is under 5
3. Seats >= 4
4. Fuel economy > 40 mpg
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
What have we learned?
Ask questions Ensure no misunderstanding
Confirm requirements Measurable Quantifiable
Requirements gathering
I need a report showing current, good applicants.
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
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
Anything else?
How do we test
tSQLt Community tool Licence free
Redgate SQL Test Fully licenced and supported product Part of the Developer suite
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
Test Object Hierarchy
GetListOfPracticalCars
GetListOfPracticalCars
Database object
Test Class
GetListOfPracticalCars.[TestAge] Test object
GetListOfPracticalCars.[TestPrice] Test object
How does this work?
Fake everything Functions Data All other objects relied upon by the object tested
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
Demo
Downloads
http://tsqlt.org/downloads/ https://www.red-gate.com/products/sql-development/sql-test/