How to write Unit Testing for ASP.NET 2.0 under VSTS?

Hi,

I have to write unit test cases for asp.net 2.0 web application using Team System's Test project.

What I have done...

1. I created a solution under Team System

2. I added empty web site project to the above solution

3. I added Test project to the above solution.

---Solution

+Solution Items

xxxx.vsdml

xxxx.testrunconfig

+WebTestProj

+TestProject

+Properties

+References

-UnitTest1.cs

So, I have a solution, web application and Test project...upto here my life is and then pains started like.....

What I need to do .....

1. I have to create a TestMethod whether the MasterPage.master existed or not.

( we are following Test Driven Development)

2. Once I run this Test case any way it will fail,becuase I haven't create any MasterPage in my WebProj so I will go there and will create MasterPage.

3. I need to write another testcase to verify whether this master page contains required controls( like Navigation,Grid and Image controls)

I browsed around the web for above requirement but I didn't have luck and am hoping will get good response from this forum.

can any one guide me how can I do the above steps. Thanks in advance.

Thanks,

Dev




Answer this question

How to write Unit Testing for ASP.NET 2.0 under VSTS?

  • none0

    Hello
    Please see the following if you are trying to create Unit tests for ASP.Net
    How to: Create an ASP.NET Unit Test
    http://msdn2.microsoft.com/en-us/library/ms182526.aspx

    Also take a look at the following blog:
    http://blogs.msdn.com/vstsqualitytools/archive/2005/08/02/446322.aspx

    You may also want to look at the following article.

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnvs05/html/WTAuthDebug.asp

    Thanks
    Brian [MSFT]
    Microsoft Developer Support
    This posting is provided "AS IS" with no warranties, and confers no rights.


  • SlightlySlow

    Thank you for your extream support, this post helpmed a lot but still i have an open questions for other areas with unit testing and am going post those with new threads with specific heading. Once again thank you for your patiency and support.

    Anjee



  • Sivaji Srinivas Narra

    Brain,

    Thank you for your valuable information, but still my problem not solved...

    I tried according to Kevin Cogger blog implementation but no luck.

    I am getting NullReference exception for

    Page page = testContextInstance.RequestedPage;

    In my case the RequestedPage object is always showing null

    This is my code snipet....

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

    namespace TeamSystem.Pilot.UnitTests

    {

    [TestClass]

    public MySampleUnitTest

    {

    private TestContext testContextInstance;

    public TestContext TestContext
    {
    get { return testContextInstance; }
    set { testContextInstance = value; }
    }

    //ctor

    [TestMethod]

    [[UrlToTest(http://localhost:3344/TeamSystemPilotApp)]

    public void PageTest()

    {

    Page page = testContextInstance.RequestedPage;
    Button button = (Button)page.FindControl("Button1");

    Assert.AreEqual("MyTestButton", button.Text);

    }

    }

    }

    I configured my ASP.NET Development server port to 3344 ( changed dynamic port assignment feature) and I have MasterPage.master and Default.aspx in my web aplication folder and I selected Host as default from testrunconfig file under solution items and my Test requirement is

    1. Verify whether MasterPage.master contains requied controls ( like navigation,grid and some buttons) or not. I need get Page object and need find the controls with that page object. I think I explained clearly. Any help from your side

    Thanks,

    Anjee



  • bettoni

    Hello

    If you are using the ASP.Net Development server and not IIS then it appears that you did not attribute your test correctly. Take a look at the following MSDN links.
    http://msdn2.microsoft.com/en-us/library/ms182526.aspx

    http://msdn2.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.hosttypeattribute.aspx

    For Example you only have the following:
    [TestMethod]

    [[UrlToTest(http://localhost:3344/TeamSystemPilotApp)]

    However the docs state that you need the following:
    Example 1. If you are running your Web site with the ASP.NET Development Server, the attributes and values you set for an ASP.NET unit test might resemble the following:

    [TestMethod()]

    [HostType("ASP.NET")]

    [UrlToTest("http://localhost:25153/WebSite1")]

    [AspNetDevelopmentServerHost("D:\\Documents and Settings\\user name\\My Documents\\Visual Studio 2005\\WebSites\\WebSite1", "/WebSite1")]

    If you follow the steps in the first link it should create the correct attributes for your test method.

    Thanks
    Brian [MSFT]
    Microsoft Developer Support
    This posting is provided "AS IS" with no warranties, and confers no rights.


  • Izak

    Hi,

    Now I am able to get the page object by doing the code like this...

    [TestMethod()]

    [HostType("ASP.NET")]

    [UrlToTest("myurl\default.aspx")]

    [AspNetDevelopmentServerHost("webapp physical dir", "/WebApprootdir")]

    public void mySamplTest()

    {

    Page page = testContextInstance.RequestedPage;

    }

    still I am not succeeded to get the title name of the page.

    I have a masterpage.master and default.aspx ( which is content page) and am trying to get the title text throuth the page .

    The masterpage's title attribute is assigned as "master page" in masterpage.master and default.aspx's title attribute is assigned as "testpage" and inthe default.aspx.cs's Page_Load handler am changing the page title to "changedtestpage" as follows

    this.Page.Title = "changedtestpage";

    and am trying to get title from my test funcation as follows

    Page page = testContextInstance.RequestedPage;

    string strTitle = (string)page.Title;

    But strTitle value is alway getting only "testpage" not "changedtest". Any help

    Thanks,

    anjee



  • Val Savvateev

    I am also struggling with the same problem, sort of. I can get it to work if my page is directly under the root, but if a page is under a subfolder, say, 'main' I continue to get a null reference exception. Even if I do something like this:

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("J:\\My Documents\\Visual Studio 2008\\Projects\\WSSMZoneSolution\\WSSMZoneWeb", "/main")]
    [UrlToTest("http://localhost:2611/main")]
    public void WelcomeTest() {
    Page pg =testContextInstance.RequestedPage;
    Label label = (Label) pg.FindControl("lblWelcome");
    String expected = "Welcome";
    String actual = label.Text;
    Assert.AreEqual(expected, actual);
    }

    Any ideas It's driving me absolutely nuts... The label's text is set at design time to, "Welcome".

  • ryan_scanner

    Hello
    The reason that you don't see your title change is that you have not called invoke on your Page_Load method. Remember this is a Unit test so if you want code in a method to run you must call it. For example I want to call my Page_Load and then check the value of my Page.Title I could place the following code in my TestMethod

    Page mypage = testContextInstance.RequestedPage;
    PrivateObject po = new PrivateObject(mypage);
    po.Invoke("Page_Load", mypage, EventArgs.Empty);
    Assert.AreEqual("Master Title", mypage.Title.ToString());

    Thanks
    Brian [MSFT]
    Microsoft Developer Support
    This posting is provided "AS IS" with no warranties, and confers no rights.


  • How to write Unit Testing for ASP.NET 2.0 under VSTS?