Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Using REDIPS.drag to add drag and drop to your .Net webapplication #li #dib0 http://t.co/n8zY3s7d
about 7 days ago http://t.co/cknQcDbo #Kindle
about 15 days ago Freedom isn't the ability to choose what to do or say, but the ability to choose what not to do or say #freedom
about 29 days ago http://t.co/61KTQknI #Kindle
12 Apr 2012 Force the use of a networking adapter using C# #li #dib0 http://t.co/ZTJOPzOz
9 Apr 2012 Mandriva 2010.2 and USB devices in Virtualbox http://t.co/fwq9gbHB
9 Apr 2012 Execute a http request to you own site with PHP http://t.co/DIvWPrpd
Home Architecture, security and coding Should unit tests use a database connection to test stored procedures?
Should unit tests use a database connection to test stored procedures?
Written by Division by Zero   
Wednesday, 07 July 2010 13:02

Should unit tests use a database connection to test stored procedures? This was the subject of a lively discussion I had with a collegue. At my office we use unit tests in our daily builds to test our software and determine the code quality. My collegue wants to test his stored procedures in these test. In my opinion this isn't a good idea.Lego bricks

Let's start with the criteria for good unit tests as described by Andrew Hunt and David Thomas in Pragmatic Unit Testing in C# with NUnit.

  • Automatic (test to be able to run without any extra installation or setup. Testing stored procedures implies some sort of database setup or cleanup before the actual test can be performed)
  • Thorough (Thorough testing implies that stored procedures should be tested within these unit tests. On the other hand business objects, in code, can be referenced directly. Stored procedures are indirectly referenced. If the stored procedures should be tested in a thorough test is still debatable)
  • Repeatable (Test must be repeatable. This means that the effects of these tests must not intefere with each other)
  • Independent (Test must be able to run independently. The database is likely to be shared among the tests. This makes the test rely on a dependent set of objects and much easier to create dependent tests)
  • Professional (There's no real issue here with testing stored procedures. The only thing can be performance, because databases are quite slow in comparison to 'pure code' tests)


Looking at these criteria, unit tests can be compared to scientific tests. You want to be able to predict the outcome, therefore you (try to) minimise the variables that influence that outcome. By comparing the real outcome to the predicted outcome you have the proof that something works or doesn't work. By adding a database connection to you unit test you add a great number of variables that you have no control of. Variables like the tcp/ip connection/stack, network device or loopback device, operating system, etc. The prediction of the outcome depends on the correct working of al these variables, making it very hard to predict. This decreases the value of the unit test.

Actually if you test more than the code you write, you are not writing unit tests, but integration tests. A database is another server, even if it's installed on the same hardware. Of course you want to test the stored procedures. You will test these in the integration test that you, as a professional developer, perform. Realize the difference and keep unit tests small and controlable. This way the unit tests have the most value to the development process. If every independent small part is proven to work, the whole is far more likely to work.

 

Add comment


Security code
Refresh

Beware of bugs in the above code; I have only proved it correct, not tried it. - Donald E. Knuth


© 2009 - 2012, Division by Zero

Template based on the empire template by joomlashack 

Valid XHTML 1.0 Strict  Valid CSS!  Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.