Unit Testing Using Visual Studio 2010
page 4 of 5
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28042/ 37

Step 3: Unit Test the Web Page

Now that we have successfully tested our CalculateDistance method we can test our web page to make sure the btnCalculate_Click event is functioning properly.

1.    Open the code behind page for the Default.aspx page.

2.    Right click in the btnCalculate_Click event and select "Create Unit Test…" from the pop-up menu.  The Create Unit Tests dialog will appear.

3.    Click the OK button.  Visual Studio will add a new test class to the TripCalculatorTest project called DefaultTest.cs.

4.    Add the following using statements to the top of the DefaultTest.cs file.

using System.Web.UI;
using System.Web.UI.WebControls;

5.    Scroll down in the DefaultTest class to the btnCalculate_ClickTest method.  Notice the attributes associated with this method.  They tell the unit testing framework which page to launch when running this test.

6.    Replace the code within the method with the following code.

public void btnCalculate_ClickTest()
{
    Page page = TestContext.RequestedPage;
 
    TextBox txtTime = (TextBox)page.FindControl("txtTime");
    txtTime.Text = "5";
 
    TextBox txtSpeed = (TextBox)page.FindControl("txtSpeed");
    txtSpeed.Text = "6";
 
    PrivateObject privateObject = new PrivateObject(page);
    Button btnCalculate = (Button)page.FindControl("btnCalculate");
    privateObject.Invoke("btnCalculate_Click", btnCalculate, EventArgs.Empty);
 
    Label lblDistance = (Label)page.FindControl("lblDistance");
 
    Assert.AreEqual("30", lblDistance.Text);
}

This code will first get a reference to the page that is being tested.  It then gets a reference to the two textboxes on the web page and sets their text properties.

The code then creates an instance of an object called a PrivateObject.  This object is in the Microsoft.VisualStudio.TestTools.UnitTesting namespace.  This object will allow you to access private methods or objects in your code.  You should never change the scope of an object just so you can test it.

The Invoke method of the PrivateObject is called to fire the btnCalculate_Click event.  The code then checks that the text of the lblDistance label matches the expected result.  If you right click in this test method and select Run Tests from the pop-up menu you will run this test.


View Entire Article

User Comments

Title: 404 on link to code for this sample   
Name: Chase
Date: 2012-11-29 1:40:46 PM
Comment:
I am getting a 404 when I click the link for the code for this sample. I'd love to try this!
Title: unit testing   
Name: Avinash Chowdary
Date: 2012-06-01 8:03:30 AM
Comment:
good article and what about the asp.net web applications they don't have .Dll to refer in test project can you explain this scenario
Title: nice one   
Name: Abhishek goletar
Date: 2012-04-16 6:18:05 AM
Comment:
nice article
thks
Title: Positive Feedback   
Name: Hitesh
Date: 2012-04-16 6:15:45 AM
Comment:
very best example to undesrstand.
I like it very much..
Thank you !!!!
Title: nice article   
Name: gourik
Date: 2011-05-03 5:02:26 AM
Comment:
thanks..
Title: unit testing   
Name: nitin garg
Date: 2011-04-06 9:00:54 PM
Comment:
good article. expecting to dive deeper in next article by creating more complex test cases.
Title: Unit Testing   
Name: wasim
Date: 2011-04-06 2:00:12 PM
Comment:
awesome.
Title: Unit Testing Answer   
Name: Vince Varallo
Date: 2011-04-06 9:16:41 AM
Comment:
The purpose of unit testing is to validate that the code you wrote performs as expected. In this example the CalculateDistance method has a few different expected results depending on the parameters that were passed in. As a developer you should test all of the expected results. If at a later date you make a change to the method you still need to validate that all of the expected results are being produced. This tool allows you to automate the process of testing your code so it should be easier to maintain and ensure the application is working as expected.
Title: Unit Testing   
Name: Dhairya Shukla
Date: 2011-04-06 2:10:55 AM
Comment:
It's good understand for me, but If you can describe what is the purpose of unit testing in asp.net and how can use it.
One more think is that explain in basic and simple language






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 7:26:44 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search