iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world
TRANSCRIPT
![Page 1: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/1.jpg)
[Database unit tests](in the SQL Server world)[Marian Chicu][Centric IT Romania]
[20th of April 2013]
![Page 2: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/2.jpg)
Who am I?• Development DBA @ Centric IT Romania
• Previously at ThinSlices and Premium Software
• Working experience with SQL Server 2000 -> 2008 R2
• http://dba.stackexchange.com/users/418/
• http://www.linkedin.com/in/marianchicu/
![Page 3: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/3.jpg)
Agenda• Definition of unit testing• Test driven development concepts• Who cares?• Benefits of unit testing• Cost of unit testing• Choosing what to test• Demo with code• Q&A (optional)
![Page 4: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/4.jpg)
What is unit testing?
“In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use.Intuitively, one can view a unit as the smallest testable part of an application.”
![Page 5: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/5.jpg)
What is unit testing?
• Unit testing is a lot like going to the gym
![Page 6: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/6.jpg)
![Page 7: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/7.jpg)
Early reactions
Developers:• I don’t test. I write code. I create.
![Page 8: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/8.jpg)
Early reactions
Managers:• I don’t want to pay developers to test, that’s why we have testers!
![Page 9: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/9.jpg)
The beginnings
XUnit = {SUnit, JUnit, CppUnit, NUnit, ...}
![Page 10: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/10.jpg)
XUnit ArchitectureTest case
This is the most elemental class. All unit tests are inherited from here.
![Page 11: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/11.jpg)
XUnit ArchitectureTest suites
A test suite is a set of tests that all share the same fixture. The order of the tests shouldn't matter.
![Page 12: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/12.jpg)
XUnit ArchitectureTest fixtures
A test fixture (also known as a test context) is the set of preconditions or state needed to run a test. The developer should set up a known good state before the tests, and return to the original state after the tests.
![Page 13: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/13.jpg)
XUnit ArchitectureTest execution
• Setup – prepare test data
• Body of test - Here we make all the tests
• Assertions - An assertion is a function that verifies the behavior of the unit under test.
![Page 14: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/14.jpg)
XUnit ArchitectureTest execution
• Assertions - Failure of an assertion typically throws an exception, aborting the execution of the current test.
• Teardown - we should clean up our 'world' to not disturb other tests or code
![Page 15: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/15.jpg)
Who cares? Why do they care?
• Developers
![Page 16: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/16.jpg)
Who cares? Why do they care?
• Developers
![Page 17: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/17.jpg)
Who cares? Why do they care?
• DBAs
![Page 18: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/18.jpg)
Who cares? Why do they care?
• DBAs
![Page 19: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/19.jpg)
Who cares? Why do they care?
• QA engineers / Testers
![Page 20: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/20.jpg)
Who cares? Why do they care?
• QA engineers / Testers
![Page 21: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/21.jpg)
What is a unit test?
• Code testing code
![Page 22: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/22.jpg)
What is the ideal unit test?
• easy to roll out
![Page 23: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/23.jpg)
• easy to roll out
![Page 24: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/24.jpg)
What is the ideal unit test?
• easy to maintain
![Page 25: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/25.jpg)
![Page 26: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/26.jpg)
What is the ideal unit test?
• run fast
![Page 27: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/27.jpg)
![Page 28: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/28.jpg)
The old way
• Print• Select statements all over• Console.WriteLine• Create executable
![Page 29: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/29.jpg)
The new way
• Visual Studio database project• Add unit test to relevant procedures• Have all tests green! Or not .
![Page 30: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/30.jpg)
Benefits
• Find problems early
![Page 31: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/31.jpg)
Benefits
• Facilitates change
![Page 32: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/32.jpg)
Benefits
• Simplifies integration
![Page 33: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/33.jpg)
Benefits
• Documentation
![Page 34: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/34.jpg)
The cost $$
The time spent actually writing unit tests in the first place.The time spent fixing and updating unit tests.The tendency to avoid improving and refactoring code.
![Page 35: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/35.jpg)
![Page 36: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/36.jpg)
![Page 37: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/37.jpg)
Real Benefits
• Thinking-driven development• Questions-driven development
![Page 38: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/38.jpg)
Test driven developmentSome key concepts
![Page 39: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/39.jpg)
Test driven development
The test system should support the developer, not the other way around.A developer must be able to create a new test in under 10 minutes.
![Page 40: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/40.jpg)
Test driven development
The test suite should be capable of running hundreds of tests in minutes (not hours).
![Page 41: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/41.jpg)
What to test?
Complex queries need better documentation
Complex queries need possible rewrites (performance tuning, anyone? )
![Page 42: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/42.jpg)
Demo Unit test
Coding demo
![Page 43: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/43.jpg)
Demo Unit test
Tools• Visual Studio 2010 Premium• SQL Server 2008 R2
![Page 44: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/44.jpg)
SSIS Unit test runner• A hidden gem• XML based
•Complex modules are more difficult to change if the requirements have changed
![Page 45: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/45.jpg)
Database unit testing tools• tSQLt – “The Database Unit Testing Framework for SQL Server” (.NET)
• DBTestUnit – “free database unit testing framework for .NET – currently supporting MS SQL, MySQL and Oracle.”
• T.S.T. the T-SQL Test Tool – a testing API (TSQL)
![Page 46: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/46.jpg)
References:Alex Kuznetsov’s blog – SimpleTalk and SQLBlog
The RedGate Guide to SQL Server Team Based Development - Phil Factor & co
What is the #1 Benefit of TDD? - Gil Zilberfeld
![Page 47: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/47.jpg)
References:XUnit – Martin Fowler
Verifying Database Code by Using Unit Tests – MSDN
Database Unit Testing with Team Edition for Database Professionals - MSDN
![Page 48: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/48.jpg)
References:PluralSight courses
• Visual Studio 2010 Database Projects• Test First Development • Unit Testing with MSTest
Selective Unit Testing – Costs and Benefits - Steven Sanderson
Unit Testing Myths and Practices - Tom Fischer (on SimpleTalk)
![Page 49: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/49.jpg)
Q&A
![Page 50: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world](https://reader036.vdocuments.net/reader036/viewer/2022062703/554fb7c8b4c90542018b45a8/html5/thumbnails/50.jpg)
[Database unit tests]
[Marian Chicu][Centric IT Romania]
[20th of April 2013]
Please fill in your evaluation form!