Tuesday, August 24, 2010

Think you can’t unit test ASP.Net? Here’s a tutorial for one way, using the power of Pex and Moles

Peli's Farm - Pex, Stubs, Moles, QuickGraph, MbUnit, Reflector Addins - New! Unit Testing Asp.NET Applications with Pex and Moles Tutorial

“We have added a new step-by-step tutorial to help testing Asp.NET applications with Pex and Moles. You will find as part of the installer or simply at …” [GD: Click through for the link]

From the PDF:

“…

This tutorial provides an introduction to writing isolated unit tests for Asp.NET applications by using:

  • Microsoft Moles 2010, which supports unit testing by providing isolation by way of detours and stubs. The Moles framework is provided with Microsoft Pex, or can be installed by itself as a Microsoft Visual Studio® 2010 add-in.
  • Microsoft Pex 2010, which automatically generates test suites with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications.

This tutorial is Technical Level 300. This tutorial assumes you are familiar with developing .NET applications and are building solutions with Asp.NET. To take best advantage of this tutorial, you should first:

Asp.NET Applications.
Asp.NET is a free web framework that enables great Web applications to be written on top of the .NET framework.

The Unit Testing Challenge.
The primary goal of unit testing is to take the smallest piece of testable software in your application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Unit testing has proven its value, because it often helps finding many defects in an early phase of the software development.

In order to execute unit tests early, you must isolate your production code from the environment. The most common approach to isolation is to write drivers to simulate a call into that code and create stubs to simulate the functionality of classes that the code uses. This can be tedious for developers and might cause unit testing to have a lower priority in your testing strategy.

It may be difficult to create unit tests for Asp.NET web applications because:

  • You cannot execute the intrinsic Asp.NET types, such as HttpContext, HttpRequest, HttpResponse, HttpRuntime, without being connected to a live IIS Server.
  • The intrinsic objects —including classes such as HttpContext and HttpRuntime—do not allow you to inject fake service implementations, because these classes are sealed types with non-public constructors.

Abstractions for intrinsic Asp.NET types.
Acknowledging the testability issue, Microsoft has provided a set of abstractions, such as HttpContextBase, HttpRequestBase, and so on…. These abstraction types allow you to author testable Asp.NET components. They are located in System.Web.Abstractions.dll for .NET 3.5 and in System.Web.dll in .NET 4.0. This document focuses on the untestable intrinsic Asp.NET types that are still widely used in the field and in legacy web applications.

Unit Testing for Asp.NET applications: Behaviors Powered by Pex and Moles.
This tutorial introduces you to processes and concepts for testing Asp.NET applications. The unit testing process uses:

  • Microsoft Moles—a testing framework that allows you to isolate .NET code by replacing any method with your own delegate, bypassing any hard-coded dependencies in the .NET code.
  • Behaviors for Asp.NET — a library that redirects Asp.NET API calls to an in-memory model of the actual Asp.NET functionality.
  • Microsoft Pex—an automated testing tool that exercises all the code paths in .NET code, identifies potential issues, and automatically generates a test suite that covers corner cases.

image…”

Interesting…

 

Related Past Post XRef:
Playing with Pex is just a web browser away at www.pexforfun.com

Pex 2010 (aka Pex 0.90.50303.0) now available on MSDN Subscribers Downloads
Pex license updated – You can now use it in commercial projects
Pex 0.5 Released - Your Parameterized Unit Test Buddy

No comments:

Post a Comment

NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...

ALL comments are moderated. I will review every comment before it will appear on the blog.

Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...

I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...

Please see my comment policy for more information if you are interested.

Thanks,
Greg

PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...