Date post: | 11-May-2015 |
Category: |
Technology |
Upload: | codecampiasi |
View: | 323 times |
Download: | 1 times |
[Database unit tests](in the SQL Server world)[Marian Chicu][Centric IT Romania]
[20th of April 2013]
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/
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)
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.”
What is unit testing?
• Unit testing is a lot like going to the gym
Early reactions
Developers:• I don’t test. I write code. I create.
Early reactions
Managers:• I don’t want to pay developers to test, that’s why we have testers!
The beginnings
XUnit = {SUnit, JUnit, CppUnit, NUnit, ...}
XUnit ArchitectureTest case
This is the most elemental class. All unit tests are inherited from here.
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.
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.
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.
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
Who cares? Why do they care?
• Developers
Who cares? Why do they care?
• Developers
Who cares? Why do they care?
• DBAs
Who cares? Why do they care?
• DBAs
Who cares? Why do they care?
• QA engineers / Testers
Who cares? Why do they care?
• QA engineers / Testers
What is a unit test?
• Code testing code
What is the ideal unit test?
• easy to roll out
• easy to roll out
What is the ideal unit test?
• easy to maintain
What is the ideal unit test?
• run fast
The old way
• Print• Select statements all over• Console.WriteLine• Create executable
The new way
• Visual Studio database project• Add unit test to relevant procedures• Have all tests green! Or not .
Benefits
• Find problems early
Benefits
• Facilitates change
Benefits
• Simplifies integration
Benefits
• Documentation
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.
Real Benefits
• Thinking-driven development• Questions-driven development
Test driven developmentSome key concepts
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.
Test driven development
The test suite should be capable of running hundreds of tests in minutes (not hours).
What to test?
Complex queries need better documentation
Complex queries need possible rewrites (performance tuning, anyone? )
Demo Unit test
Coding demo
Demo Unit test
Tools• Visual Studio 2010 Premium• SQL Server 2008 R2
SSIS Unit test runner• A hidden gem• XML based
•Complex modules are more difficult to change if the requirements have changed
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)
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
References:XUnit – Martin Fowler
Verifying Database Code by Using Unit Tests – MSDN
Database Unit Testing with Team Edition for Database Professionals - MSDN
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)
Q&A
[Database unit tests]
[Marian Chicu][Centric IT Romania]
[20th of April 2013]
Please fill in your evaluation form!