Title: Webbased Unit Testing with NUnitASP
1Web-based Unit Testing with NUnitASP
- Web-based Unit Testing with NUnitASP
- September 29th, 2005
Mark Simms ltmark.simms_at_cathexis.comgt
2Overview
- Overview
- Introduction to Unit Testing with NUnit
- Introduction to NUnitASP
- Sample project and hands-on
- Top Secret Debugging Techniques ?
3Objectives
- To provide a brief introduction to unit testing
in a web-centric environment - Provide a step-by-step walkthrough for
implementing NUnitASP tests - To not put anybody to sleep.
- To show some Top Secret techniques for
troubleshooting and debugging
4Introduction to NUnit
- NUnit is a unit-testing framework for all .Net
languages - NUnit automates the execution of small, unit
tests which exercise a component of code - Can build up libraries of NUnit code into
regression suites. - Open-source, downloadable from www.nunit.org
5How does it work?
A TestFixture is a collection of tests
using NUnit.Framework TestFixture public
class SomeTests SetUp public void
SetUp() TearDown public
void TearDown() Test
public void TestOne() // Do some
stuff here
The SetUp attribute means that this function
runs before each of the other tests is called
The Test attribute means that this function
implements a test.
6How does it work?
internal class Class private int _value
public int Value get return
_value set _value value
Test public void TestOne() Class c
new Class() c.Value 5 int x
c.Value Assert.AreEqual(5, x)
A silly little class. We want to test the Value
property
In our TestOne() test we create an instance of
this silly little class
We set the value to 5, then read it back
Bonus points what is a potential problem with
this test?
7Hands-On NUnit for Database Testing
- Have a SQL Server database containing
- Contacts table
- AddContact stored procedure
- Want to have
- Automated, repeatable testing
- Validation of
- Standard conditions
- Boundary conditions
- Error conditions
8Introduction to NUnitASP
- NUnitASP is a set of NUnit extensions for easy
access to ASP.NET - Designed to abstract away all of the details of
- Loading up a browser
- Interpreting HTML
- Interacting with page elements
- Text boxes
- Buttons
- Data grids
9Introduction to NUnitASP
- Downloadable from
- http//nunitasp.sourceforge.net/
10NUnitASP Classes
Standard NUnit test case
Test public void TestExample()
// First, instantiate "Tester"
objects LabelTester label new
LabelTester("textLabel", CurrentWebForm)
LinkButtonTester link new
LinkButtonTester("linkButton", CurrentWebForm)
// Second, visit the page being
tested Browser.GetPage("http//localh
ost/example/example.aspx") //
Third, use tester objects to test the page
AssertEquals("Not clicked.", label.Text)
link.Click()
AssertEquals("Clicked once.", label.Text)
link.Click() AssertEquals("Click
ed twice.", label.Text)
Test objects for working with ASP.NET elements
Integrated browser object
Check the text in an ASP.NET textbox
Click a button on a web form
11Hands On Test Web Application
- Wrote a simple web application that
- Allows users to add a contact (which in turn
calls the AddContact stored procedure) - Allows users to list contacts
- Note the previous tests were for Data and Logic
layers. NUnitASP allows testing of the
presentation layer in web-based applications.
12Hands On Test Web Application
- Test 1
- Load the AddContacts.aspx page
- Confirm that the Name and Email textboxes are
visible - Test 2
- Call Test 1
- Fill in the Name and Email boxes with valid
entries - Submit form
- Confirm data is in database
13Hands On Test Web Application
- Test 3 (error condition)
- Call Test 1
- Submit form
- Expect error label to be visible
14Top Secret Funky Debugging Techniques
- Process tracing using Sysinternals tools
- Filemon
- Regmon
- Will demonstrate use of Filemon to track down
- TypeInitializerException
- errors