简体   繁体   中英

Beginners introduction to unit testing in Visual Studio 2008

I'm a self-taught developer and my experience is all in small applications that I've developed.

I'm currently working on an application that I've made public, and I've realized that I need to start doing good unit testing to catch regressions and generally make sure everything works.

I've read up on a previous question . I would like to know if there are any resources online specifically dealing with C# unit testing in Visual Studio 2008, preferably with examples.

EDIT: I'm using Visual Studio 2008 Professional for Windows applications, no web development.

You don't specify which flavor of VS2008 you are using. If it is Pro or above, then MSTest is bundled, but a lot of people have issues with it - it isn't always very intuitive, and it takes far too much setup to do simple things like coverage / file deployment.

A walkthrough is here .

As a recommendation, I suggest using VS2008 with NUnit (free) and TestDriven.NET (not free). It takes away all the pain, allowing you to just write simple things like:

[TestFixture]
public class Foo {
    [Test]
    public void Bar() {
        Assert.AreEqual(2, 1+1);
    }
}

Then just right-click (on the class, on the method, on the project, on the solution) and use the Test options that TestDriven.NET provides, including (if you have MSTest) "Test With -> Team Coverage", which runs your NUnit tests with the MSTest coverage tools, including giving the colorization back into the IDE to show which lines executed. No messing with "testrunconfig" and the other files that MSTest wants you to use.

http://www.asp.net/learn/mvc-videos/

Storefront and the Pair Programming videos involve a lot of TDD (Test Driven Development)

You don't specify whether you're working on web/windows apps, but if you're learning ASP.NET as well then Richard Dingwall has some excellent links to example asp.net mvc applications that use TDD. There are examples using nUnit as well as mocking frameworks such as Rhino.Mocks and Moq.

Have a look at his blog post here for some links:

http://richarddingwall.name/2008/11/02/best-practice-dddtdd-aspnet-mvc-example-applications/

James Gregory posted a pretty good primer on unit testing in general here:

http://blog.jagregory.com/2007/07/17/getting-with-it-test-driven-development/

Hope this helps!

The e-book Foundations of programming: http://codebetter.com/blogs/karlseguin/archive/2008/06/24/foundations-of-programming-ebook.aspx

also covers unit testing.

如果您不仅仅对正常的单元测试感兴趣,那么请看看PEX

I would recommend looking at screencasts, to get a feel for how TDD is applied. At Dnr TV there are two episodes with JP Boodhoo, where he gives an introduction to test driven development:

If you want to see unit testing and TDD used together with a whole bunch of other agile practices, I would recommend watching the sceencast series Autumn of Agile . This series shows the development of a fully unit tested application from start to finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM