I changed my mind - Mock objects are wrong for database unit testing - ISerializable - Roy Osherove's Blog

I changed my mind - Mock objects are wrong for database unit testing

More than a year ago I wrote about the problems in database testing. I also stated that I feel it's totally wrong to run your unit tests against a live test database, but instead you should be using mock objects to save you from all the trouble.
Boy have things changed since then. I take back what I said. For a long time now I've been doing DB testing without mock objects, because I believe that's currently the best way to make sure you test data access logic and integration with the database procedures and views.
There are now several easy to implement solutions to the "how-the-hell-do-I-roll-back-the-changes-i've-made-to-the-db" problem(see this article, mbUnit , xtUnit and this one about extending the new Nunit as well for several options for doing the same thing).
With those - there's no reason for you not to do it.
Funny how things change overtime. Like any other thing, but usually more, software development is one of those things that looking back 6 months from now I'll always say to myself "Gosh! if I knew then what I now!"..
Published Friday, December 10, 2004 3:04 AM by RoyOsherove
Filed under:

Comments

Thursday, December 09, 2004 5:30 PM by TrackBack

# Are mock objects wrong for database unit testing

Are mock objects wrong for database unit testing
Thursday, December 09, 2004 8:41 PM by TrackBack

# I changed my mind - Mock objects are wrong for database unit testing

Friday, December 10, 2004 1:17 AM by TrackBack

# Il peccato originale della community .NET?

Friday, December 10, 2004 1:21 AM by TrackBack

# Il peccato originale della community .NET?

Friday, December 10, 2004 4:27 AM by Anthony Williams

# re: I changed my mind - Mock objects are wrong for database unit testing

I agree that you need to test against a live database, but I think that testing against a mock database is also a good thing.

The reason? They test different things. Testing against a mock database tests that the code makes the requests you expect, and handles the data how you expect. Testing against the live database verifies that this aligns with how the data really is.
Friday, December 10, 2004 11:56 AM by talav

# re: I changed my mind - Mock objects are wrong for database unit testing

Mock objects only allow to test the sequence of calls to test the logic of your application


Live database is a must especially when youre talking about complicated data entry application

Mianly for:
1. Validating that the database is not curropt with its logic

2. testing for performance issues your application

3. testing on all kinds of data scenarios your code.
Friday, December 17, 2004 6:32 PM by Yosi Taguri

# re: I changed my mind - Mock objects are wrong for database unit testing

Hate to say it but:
Told You So ;)
Monday, December 20, 2004 10:57 AM by Philip Nelson

# re: I changed my mind - Mock objects are wrong for database unit testing

I agree that the techniques you promote here are valuable techniques, and we have been using them for over a year. I have also come the acceptance that a first choice for working with unit tests and database supported code could/should be a test database. But, I can tell you with absolute certainty that this solution does not work well on large projects. It's most reliable when using the rollback type solution, but this also represents the worst case for speed of the tests. Other options like restoring the database to a known state are problematic, though I have been experimenting with techniques to make that faster than a full restore allows. Having the tests cleanup after themselves can be made to work with a lot of discipline, but handling all of the exception cases makes that approach pretty hard to be rock solid reliable.

My current test suite. based mostly on test databases and manual cleanup and rollback techniques, takes over 45 minutes to run 8^(
Tuesday, December 28, 2004 12:11 PM by John Redfern

# re: I changed my mind - Mock objects are wrong for database unit testing

I disagree, but...

Testing against a live database is a good thing to make sure that your code integrates well with your data source. However, this is not a "unit" test -- it's an "integration" test.

Integration tests are good for various reasons and should be run now and then, but they are typically way too slow for a good set of unit tests. Different people have different objectives and practices, but I like to run all the unit tests very often... typically after changing just a few lines of code. Using a live database makes this unproductive and unbearable.

Check out Rainsberger's "JUnit Recipes". He has some good suggestions for this sort of testing.
Wednesday, February 14, 2007 1:56 AM by .RICHARD » Blog Archive » Testable data access code

# .RICHARD » Blog Archive » Testable data access code

# .RICHARD » Blog Archive » Testable data access code