Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

50
[Database unit tests] (in the SQL Server world) [Marian Chicu] [Centric IT Romania] [20th of April 2013]

Transcript of Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

Page 1: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

[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

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

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

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

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
Page 7: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

• easy to roll out

Page 24: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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
Page 26: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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
Page 28: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

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

Benefits

• Find problems early

Page 31: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

Benefits

• Facilitates change

Page 32: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

Benefits

• Simplifies integration

Page 33: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

Benefits

• Documentation

Page 34: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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
Page 36: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world
Page 37: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

Test driven developmentSome key concepts

Page 39: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

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

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

Demo Unit test

Coding demo

Page 43: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

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

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

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 48: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

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

Q&A

Page 50: Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql server world

[Database unit tests]

[Marian Chicu][Centric IT Romania]

[20th of April 2013]

Please fill in your evaluation form!