ProMesh.NET Quick Walkthrough - Part 9 - Unit Testing


One of the most powerful and useful features of ProMesh.NET is its built-in offline mode for unit testing. ProMesh.NET allows you to write methods that mimic a user navigating through the site. This allows you to write unit tests for your complete website, which is almost impossible with ASP.NET or other frameworks.

Consider the following scenario we want to test:
  • Navigate to a page which is not accessible for a user who is not logged in yet
  • Check if the application correctly redirected us to the login page
  • Enter the login and password of a known user
  • Check if the application redirected to the main page for authenticated users

This is the NUnit test fixture you could write for testing the scenario described above:

[TestFixture]
public class WebSiteTest
{
   [Test]
   public void TestLogin()
   {
            OfflineWebSession webSession = new OfflineWebSession();

            webSession.FollowRedirects = true;

            // try to open a page not allowed for anonymous users
            webSession.PageGet("/youraccount.ashx");

            // check if page was redirected to the login page
            Assert.AreEqual(webSession.CurrentPage, "/login.ashx");

            // enter the login and password on the login page
            webSession.PostData["LOGIN"] = "john@doe.com";
            webSession.PostData["PWD"] = "mymostsecretpassword";

            // do login
            webSession.PagePost("/login.ashx");

            // check if the page was redicted to the "My Account" page
            Assert.AreEqual(webSession.CurrentPage,"/youraccount.ashx");

            // check if the correct user is logged in
            Assert.IsNotNull(WebApp.Session["USER_ID"]);
            Assert.AreEqual(9,WebApp.Session["USER_ID"]);

            // ALL TESTS PASSED
   }
}


You can see right away that the possibilities are endless. You don't have to do anything specific in your application to allow unit testing. Just create a test fixture and perform the actions you want to test. That's it.

Last edited Aug 2, 2007 at 8:46 PM by activa, version 3

Comments

No comments yet.