views:

87

answers:

4

How do people run their automated tests for ASP MVC ?

Currently we're using native Visual Studio unit tests and running them linearly on a single machine. They're too slow to be useful currently.

Switching to nunit? Distributing the unit tests with Incredibuild XGE? Anyone tried these or have other ideas?

Thanks.

A: 

changing the unit testing framework isn't going to do you any good

if your tests take too long mostly because they connect to database or a web service ...

i suggest you use a CI to run all the tests also separate tests for example the UI layer should have it's tests separated from the data access tests ...

Yassir
+3  A: 

Most of the speed hit you are seeing is MSTests firing up the ASP.NET development server every time you want to run even a single test.

I keep as much of my application as possible in separate libraries outside of my ASP.NET application, so that they can be unit tested independently. This avoids the hit you take when it fires up the ASP.NET development server during unit testing.

When mocking controllers, I don't see any reason why you couldn't remove the attributes that tell the testing system to fire up the development server. After all, isn't the whole purpose of mocking to take out the "outside" components?

Similarly, the whole purpose of using repositories for your models is so that you can inject mocking objects for testing. These tests don't need the development server either.

As far as the views go, I wouldn't write unit tests for these. Keep them as thin as possible, and test them manually by visual inspection.

A different set of tests can be included for your models and controllers that include the ASP.NET development server. These tests would be part of your Integration Test suite.

Robert Harvey
+3  A: 

The problem with MSTest isn't the run speed itself, it's the test environment itself. You can run MSTest tests with Resharper and they are quite speedy. My tests run against a repository interface, and I mock an in-memory repository when invoking the controllers.

That said, you controller tests should be very light: return type (ViewResult, JsonResult) and model type. Not much else (see http://www.arrangeactassert.com/how-to-unit-test-asp-net-mvc-controllers/). Test your business tier calls separately from your controllers. Done correctly, a thousand unit tests take just a few seconds to complete.

Jarrett Meyer
A: 

Test speed is affected by several factors -- them being MSTest may actually be just one of them.

Any of the following can have an effect on test speed:

  • Tests that actually perform db access
  • Tests that depend on disk access (e.g., reading web.config)
  • Code that is actually broken (e.g., performing countless unnecesary loops, other performance loopholes)
  • Tests that do too much (testing for several things at a time) or doesn't really test a unit

Unit Tests, by definition, should be small and fast. Aside from your framework, look for other things among the above list that may be slowing them down.

Jon Limjap