views:

136

answers:

4

Now this might be a dumb question, but I'm still new to using Visual Studio 2008 but I was wondering if there is a feature like this (I'm using webforms ASP.NET C#).

I create a new method, highlight it in the editor and was wondering if there was a run/test method feature? Where it would exectute the method and prompt me via a UI for any variables the method might need to run?

I just thought this would really increase my productivity / speed in creating applications :S

+1  A: 

You can use the console window in VS to test individual methods – provided the surrounding class compiles error-free. The console window can be activated via the View menu, IIRC.

Once inside the debug console, just enter the class name, followed by the method name (if it's a static method) or create an instance, if not. Printing the result can be done by prefixing the whole thing with ?, e.g.:

> ? MyFancyStringHelperClass.Reverse("Hello")
elloH
Konrad Rudolph
It's called the "Immediate" window, under Debug - Windows in VS2008.
Thorarin
+1  A: 

If you put your business logic in a separate class, you can use nunit to write proper, repeatable unit tests. www.nunit.org

AdaTheDev
+2  A: 

I prefer to follow test driven development (TDD) when using Visual Studio - or any other language/IDE for that matter. Essentially you assert what you want your code to do by writing the unit test first, validate that the test fails, and then fill in the blanks in the method that you are testing. Its much easier to say than do, but once you're used to it, it becomes very natural and fast - not to mention your code has a lot less defects!

For a tool where you can test some code as you go, I recommend LinqPad (and there is also SnippetCompiler). While these don't let you highlight code and execute you can copy and paste into them achieving much of the same results.

For writing unit tests in VS you can use NUnit or any of its clones. I do not recommend the VS for Testers for unit testing.

I use NUnit in my current project and have become a fan of ReSharper for integrating the test suite into Visual Studio.

cfeduke
What is the reason you would not recommend using MSTest? I have used some NUnit in the past, but so far I have no problem with the way the builtin Visual Studio stuff does it.
Thorarin
Generating unit tests, while normally backwards (TDD afterall), could sometimes be useful if it didn't generate a ton of invasive code to get at private methods. Of course the last time I used it was with the first version so I haven't looked at it since (reading up on it indicates a lot has improved).
cfeduke
A: 

Take a look at Test Driven .NET this plugin adds functionality that you can right click a method and test it immediately. It's very useful.

Chris Marisic