Friday, October 30, 2009 6:31 AM Tanzim Saqib

Making NUnitASP work with xUnit

NUnitASP is a tool for automatically testing ASP.NET websites. It’s an application of Test-driven Development technique, commonly known as TDD. It was initially developed as NUnit extension. NUnitASP is a class library which allows you to load webpage within an invisible browser instance, lets you fill up textboxes, click buttons and verify actions afterwards. With the growing interest of ASP.NET developers, the author decided to make it any version of any testing framework compatible. I did not find it working with xUnit though, which of course is backed with the theory that he abandoned its further development. The objective of this post is to make it work with xUnit.

Here is a glimpse of how the test methods will look like:

[Fact]
public void GivenStatusUpdate_StatusLabel_ShouldUpdate()
{
    var sampleUpdate = "I am running a test using xUnit and NUnitASP";
 
    Browser.GetPage("http://localhost:8010/");
    
    // Set text
    var txtStatus = new TextBoxTester("txtStatus");
    txtStatus.Text = sampleUpdate;
 
    // Update it
    var btnUpdate = new ButtonTester("btnUpdate");
    btnUpdate.Click();
 
    // Verify it
    var lblStatus = new LabelTester("lblStatus");
    Assert.Equal<string>(lblStatus.Text, sampleUpdate);
} 
 
 

Steps to make it work with xUnit

  1. Add reference to NUnitASP.dll, xUnit.dll and nunit.framework.dll
  2. Add NUnitAdapter.cs from NUnitASP package to the Class Library project created for Test
  3. Comment out the Setup attribute in NUnitAdapter.cs. We need to do this because we are not running on NUnit.
    //[SetUp]
    public void MasterSetUp() 
    {
        setupCalled = true;
        HttpClient.Default = new HttpClient();
        SetUp();
    }

  4. Modify the AssertSetup like the following. We need to call this manually since the Setup will not be fired automatically because we are not running on NUnit.
    private void AssertSetUp()
    {
        if (!setupCalled) 
        {
            MasterSetUp();
        }
    }

  5. Write your tests and compile to an assembly
  6. Run from xUnit console runner: xunit.console.exe

 

See the nice XSLT driven HTML result produced by xUnit:

xUnitWithNUnitASP-result

Download Source Code of the Sample [xUnitWithNUnitASP]

Filed under: , , , ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)