ASP.NET unit testing

NUnitAsp v2.0 Migration Guide

In version 2.0, NUnitAsp stopped requiring specific versions of NUnit. This change has many benefits, but it isn't backwards compatible. Fortunately, there's an easy change that will enable you to keep using your current tests.

Why did we do this? The main reason is that, prior to this change, each version of NUnitAsp only worked with one specific version of NUnit. Whenever a new version of NUnit came out, there was a time lag—often several months—before a new version of NUnitAsp came out to support it. And whenever you upgraded your copy of NUnitAsp, you also had to upgrade your copy of NUnit at the same time, which you might not have wanted to do.

This change also gives you more choice in testing tools. NUnitAsp v2.0 works with NUnit, as always, but it should also work with other tools, like MbUnit. As a side benefit, this change also allowed us to make some simplifications to NUnitAsp's API that we've been wanting to make for a while.

Breaking Changes in v2.0

These changes in NUnitAsp v2.0 that could break your existing tests.

  • Assert: The Assert method has been renamed to AssertTrue. This allows you to use NUnit's Assert class more conveniently.

  • UserAgent: The default UserAgent has been changed to be the same as Internet Explorer's. This causes ASP.NET to render pages in the same way as it does with IE.

  • DataGridTester.GetRowByCellValue: We have improved the algorithm we use to strip unnecessary tags from string results. This has changed the results slightly and may require you to change some calls to GetRowByCellValue.

See the quick migration section, below, for information on how to work around these changes.

Other Major Changes in v2.0

There are a few other major changes that you may wish to take advantage of as you write new tests.

  • CurrentWebForm: The CurrentWebForm property is now optional. Instead, we have a new constructor that just takes an ASP ID. We think this will make tests a lot easier to read and write.

  • Multiple Forms: You may now test pages that have multiple forms. Simply construct a WebFormTester that describes the correct web form and pass it in as the "container" argument in the constructor.

  • HtmlTagTester and XPath: In addition to many more HTML testers, we have added a "generic" HTML tester called HtmlTagTester. You may use this tester to test HTML tags that don't have other tester support. In addition, we've added a constructor to all of our HTML testers, including HtmlTagTester, that takes an XPath as a parameter rather than ID. Use this new constructor to create testers for tags that don't have IDs.

Quick Migration

Migrating to NUnitAsp v2.0 is a simple process.

  1. Make sure all of your tests pass before upgrading to NUnitAsp v2.0.

  2. Compile the included NUnitAdapter.cs file into the assembly that contains your tests. (In Visual Studio .NET, this means copying the file into the project that contains your tests and telling Visual Studio to "include" it.) NUnitAdapter.cs is located in the "bin" directory of the NUnitAsp download package.

  3. Find all calls to "Assert" in your tests and rename them to "AssertTrue." (This can usually be done easily with Replace In Files: Ctrl-Shift-H in Visual Studio .NET.)

  4. Verify that your tests still pass.

  5. If the tests don't pass:
    • Look at the error message and check if your "actual" values have changed slightly. You may see style tags like <b> and <i> disappear. In most cases, this will be because ASP.NET is rendering the page differently due to our changed UserAgent. If so, you can simply update the "expected" value in your test.

    • Look for a call to DataGridTester.GetRowByCellValue and try changing the value you pass in to that methods. If the cell value you're looking for has a space at the beginning or end, add the space to your method call. If it contains multiple spaces in the middle, try combining them into a single space.

Complete Migration

After completing the Quick Migration above, you may gradually convert your tests over to the new NUnitAsp v2.0 test style. These steps are entirely optional.

Change DataGridTester.TrimmedCells to DataGridTester.RenderedCells

"Rendering" is a new algorithm in NUnitAsp v2.0 that allows us to convert HTML into a plain-text string that more closely represents what you see in a browser. This approach replaces previous "Trimmed" and "NoTags" methods.

DataGridTester was particularly affected by this change. We have marked the TrimmedCells property obsolete and replaced it with Cells and RenderedCells.

In many cases, you can simply replace a call to TrimmedCells with a call to RenderedCells.

DataGridTester dataGrid = new DataGridTester("dataGrid", CurrentWebForm);

string[][] expected = new string[][]
  new string[] {"expected data grid"}
AssertEquals(expected, dataGrid.TrimmedCells);


DataGridTester dataGrid = new DataGridTester("dataGrid", CurrentWebForm);

string[][] expected = new string[][]
  new string[] {"expected data grid"}
AssertEquals(expected, dataGrid.RenderedCells);

Remove CurrentWebForm

Remove any "CurrentWebForm" parameters from tester constructors.

LabelTester label = new LabelTester("label", CurrentWebForm);


LabelTester label = new LabelTester("label");

Use Assert and WebAssert

Replace calls to inherited Assertion and WebAssertion methods with calls to Assert and WebAssert.

AssertVisibility(label, true);
AssertEquals("label", "hello", label.Text);
AssertEquals("datagrid", new string[][] {new string[] {"hello"}}, datagrid.Cells);


Assert.AreEqual("hello", label.Text, "label");
WebAssert.AreEqual(new string[][] {new string[] {"hello"}}, datagrid.Cells, "datagrid");

by James Shore. Last updated for v2.0.