views:

71

answers:

3

Visual Studio 2010 generated a bunch of unit tests for me which seems to be handy, but I question whether they are actual unit tests. For example, it generated a test it called SaveTest which executes the following code:

User user = new User();  //I have to create a user
user.Save(); //This saves the user to the database.
//Assertions here....

The problem I have above is that from what I have read, unit tests are supposed to test things in Isolation, so by testing Saving to the database, this is not a unit test or am I wrong and does MsTest get it wrong? As a side note, User is generated from by dbml and Save calles SubmitChanges on my DataContext.

+6  A: 

The auto-generated tests are only unit tests if you've already done the necessary work to isolate dependencies so that the calls with side effects can be routed to the appropriate mocked or stubbed dependencies for sensing the desired effects. The tools won't do the work of architecting your code to support unit testing; all they can do is create calls to the various public access points. You'll also still have to do the work of arranging your dependency states for each test and asserting the conditions you wish to check.

Dan Bryant
ah well i was just about to press save with almost exactly the same +1 for speed.
tim
A: 

MSUnit, AFAIK, will not be intelligent enough to know how to mock behavior that it tests. As such, if you don't mock the DB dependencies of User yourself, it's not a unit test.

"Unit-testing" frameworks like MSUnit and NUnit provide little more than the environment in which to run a suite of programmatic tests. You decorate a method with the [Test] attribute and it'll run it. That test can be at the unit, integration, functional or even acceptance level, depending on the scope of code being covered by the test. In our project we have a whole assembly of nothing but integration tests, which cover whole vertical slices of functionality and even model client acceptance test cases. All of it is written using the NUnit framework.

Your development environment cannot prevent you from making mistakes in development; if that were true, there wouldn't be a need for QA.

KeithS
MSUnit? Did you mean MSTest or [MbUnit](http://www.mbunit.com)?
Yann Trevin
A: 

All these unit test frameworks aren't really constrained to do unit tests.

You can do plenty of different test types with those, including full system tests and focused integration tests. Specially as you add extra frameworks tools used from your test code / runner.

If you have a very thin class that its only responsibility is integrating with an external system, then if you create tests around it those are focused integration tests and are best done hitting the external system. Just make sure to keep them separately from the unit tests since those will be naturally slower.

If on the other hand, the class that has the hard dependency to the external system isn't just a thin class, then the issue is with your code ;). Address it, and by nature your tests won't have that issue.

eglasius