RDN Express Blog

Blog archive

Test-Driven Development in VS 2010

Microsoft is finally getting the message about the .NET community's interest in test-driven development (TDD). The result is some cool functionality in the new editor in the upcoming IDE.

By now, everyone is familiar with the ASP.NET Model View Controller (MVC) pattern, a framework that Microsoft swiftly developed after the alarming enterprise interest in Ruby on Rails. ASP.NET MVC Version 1 was released at MIX09 in March but it isn't in Visual Studio (VS) 2010 and .NET 4 Beta 1.

"We're working hard to have an out-of-band installer, which will install the project templates and tooling for ASP.NET MVC which works with VS 2010 Beta 1 some time in June on CodePlex," explained Microsoft's Phil Haack today in his blog. ASP.NET MVC will be in VS 2010 Beta 2, according to Haack.

The co-hosts of the Connected Show, New York-area Microsoft developer evangelist Peter Laudati and Microsoft consultant Dmitry Lyalin, hit on TDD yesterday during a wide-ranging VS 2010 discussion with VS product planner Tony Goodhew. Goodhew never mentioned that ASP.NET MVC was not in Beta 1 but he offered some insight into how TDD will work in the new VS 2010 editor.

At one point, Goodhew talked about academic research around the cost to the developer of reacquiring context. "When I am doing something, I have to stop doing it. The re-acquiring of the context of what I was doing from the UI can be very, very expensive in terms of developer productivity...we wanted to focus on reducing this context acquisition cost at all times."

This "in-context" strategy explains, in part, how the new editor eases TDD. Here's a bit of the TDD discussion from the show:

Laudati: With TDD, that's kind of been an up-and-coming development practice that a lot of folks have been adopting in the Microsoft community. There's been some criticism, I think, around the way Visual Studio handles some of that. I know that the MVC framework kind of makes it easier to do TDD from the development and design perspective. Is there anything new in the tooling in 2010 that addresses some of those earlier criticisms?

Goodhew: Absolutely. One of the challenges, previously, if you used Orcas [VS 2008] or Whidbey [VS 2005] to do test-driven development is that the TDD model is really around this concept that you define your test and then you confirm the test files and then you go implement your classes and you start implementing until all your tests pass. The TDD experts would flame you for that description, but that's the very high-level thumbnail sketch of what it is. Now, part of the challenge to that is you are consuming a lot of stuff that you have in that test that you have not yet written. In Orcas when you would write that test, you would lose a lot of the IDE support for that test because you get to say, "OK, I need to test my class so I need to declare the class and the test, but I haven't even actually even declared the creation of the class in the project..."

The IDE in Orcas and Whidbey forces me out of that context to do a class implementation and deal with the project system, so that by the time I've done that and get back, now I have to acquire context in something I test again.

Goodhew then went on to describe how the new "consume first, declare second" model works in VS 2010.

Goodhew: In the editor, when you are inside your test, when you go and create an MVC app, and you've created your view, and you're starting to implement a view for your test or your view for your controller, you start writing your test. You say, "OK, I am going to have this class." You just type that class name and the editor will say, "That class does not exist, would you like me to implement it and stub it out for you?"

You click a single button. It goes out, creates a file for you, sticks it in the right location in your project system, puts in all the names that you've used, wraps that into your application, so now your class is declared. Then you go and use a method on it. The method doesn't exist, so you can write -- in-context of writing your test -- have the editor support creation of the stubs for those methods for you. It allows you to keep that test context, as you're writing your code and have the IDE do all of the grunt work of basically going and starting at your class for you.

Laudati: A lot of the criticism around TDD wasn't necessarily that it was hard to do it from a framework perspective, like let's say Web Forms versus MVC. But in general, what you just described is, you write your tests first as part of the design process of designing your code, and the tooling was like a friction in the way. It sounds to me what you just described is a huge relief where the tooling is now going to help with that process by default.

Goodhew: I think the MVC pattern inherently assists in the creation of TDD stuff. It's not required, obviously, as you said, but it's much easier to do it in that sort of approach. And taking that control -- implement my test, implement my model, implement my view -- being able to separate that thinking, and then have the IDE handle the stubbing out for you, lets you keep the context of the design of your app while you're in that test creation process. So I think you're going to find that TDD development, if you use MVC or if you use any pattern, really, because this is not just constrained to Web development -- this actually is available across the editor whenever you create classes anywhere. I think people will get a lot of value for that.

Listen to the entire podcast here.

Interest in TDD is growing and a series of upcoming articles in RDN's sister publication, Visual Studio Magazine, looks at a repository pattern for the end-to-end integration test problem and unit testing user interface code with the ASP.NET MVC framework.

Are you doing test-driven development in Visual Studio/ASP.NET MVC? What issues are you running into? Express your TDD tales or questions below or contact me directly at [email protected].   

Posted by Kathleen Richards on 06/09/2009


comments powered by Disqus

Featured

Subscribe on YouTube