Silverlight-Do Unit Text in Silverlight 4.0

We need to create unit test to guarantee high quality of project in Silverlight 4.0. In this post, I will introduce the method to create a project to do unit test.

  1. Create a Silverlight application named SL4UnitAPP, not needing to load in Web.
  2. Right click SL4UnitAPP solution. Add one Silverlight Unit Test Application named SL4UnitTest.
  3. If we want to add Silverlight Unit Test Application when creating project in VS2010, we need to copy ItemTemplates and ProjectTemplates foloders under C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE to D:\My Documents\Visual Studio 2010\Templates.
  4. Then, we can create Silverlight Unit Test Application and add SL4UnitApp as reference.

Add two functions which are needed to verified and one public variable in MainPage.xaml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SL4UnitAPP

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        ///<summary>

        ///Return to String “¡ãmainPage”¡À

        ///</summary>

        ///<returns></returns>

        public string ReturnString()

        {

            return “mainPage”;

        }

        public string GetStringFromMethod;

        ///<summary>

        ///Set Public Variable as SecMethod

        ///</summary>

        public void GetString()

        {

            GetStringFromMethod = “SecMethod”;

        }

    }

}

5. Instantiate MainPage class in SL4UnitTest and then add two variables with [TestMethod] property to test if through unit test. (Note: if it turns to debug mode because of AssertionError, Press F5 to continue running and getting result. )

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Silverlight.Testing;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using SL4UnitAPP;

namespace SL4UnitTest

{

    [TestClass]

    public class Tests

    {

        MainPage mpage = new MainPage();

        [TestMethod]

        public void TestMethod1()

        {

            //Assert the result mpage.ReturnString() get is mainPage.

            Assert.AreEqual(mpage.ReturnString(), “mainPage”);

        }

        [TestMethod]

        public void TestMethod2()

        {

            //Run mpage.GetString() function firstlyê?then assert GetStringFromMethod value as SecMethod

            mpage.GetString();

            Assert.AreEqual(mpage.GetStringFromMethod, “SecMethod1”);

        }

    }

}

The first method is the right assertion and the second method is wrong assertion. Press F5 and then we can get the following two images.

 

Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s