Silverlight unit test

Source: Internet
Author: User
Tags silverlight
Document directory
  • Problem:
  • Environment Configuration:
  • Solution:
  • Conclusion:
Problem:

When we write code, it will be debugged repeatedly to ensure that it can be compiled through

However, after the code is compiled, it only shows that its syntax is correct.

But it cannot guarantee that its semantics is correct.

That is, the behavior of this Code cannot be easily guaranteed to be correct.

While writing unit tests can verify code behavior and our expectations are consistent

The sooner the unit test is used to test your code, the better.

It is best to start writing the first line of code.

So how does Silverlight Perform unit testing?

Environment Configuration:

I am very happy to tell you that Microsoft has provided us with support for unit testing.

To perform a Silverlight unit test, complete the following two steps:

1. Download The Silverlight unit test vs template and the Silverlight unit test framework binaries library.

Silverlight unit test templates(ZIP, 46 k)

Silverlight unit test frameworkBinaries (ZIP, 284 K)

Silverlight unit test document(ZIP, 1881 K)

2. decompress the downloaded Silverlight unit test templates package.

Unzip the hosts file, otherwise vs does not recognize it)

% USERPROFILE % \ Documents \ Visual Studio 2008 \ Templates \ projecttemplates

Copy the silverlighttestclass_csharp.zipto and the silverlighttestclass_vb.zip file

% USERPROFILE % \ Documents \ Visual Studio 2008 \ Templates \ itemtemplates

Decompress the downloaded Silverlight unit test framework binaries and copy the content to the following directory:

C: \ Program Files \ microsoft sdks \ Silverlight \ V2.0 \ libraries \ Client

Start vs2008. If the following template is displayed:

This indicates that our vs2008 has been configured successfully.

Silverlight unit test is supported

Solution:

Okay. Let's do some unit tests.

As shown in, create a Silverlight test project as follows:

The project contains the following files:

 

The difference between this project and the general Silverlight project is that there is no custom Silverlight control.

So what is the rootvisual of this project?

Open the app. XAML. CS file and you will see the following code:

1 private void application_startup (Object sender, startupeventargs E)
2 {
3 This. rootvisual = unittestsystem. createtestpage ();
4}

Unittestsystem is a class under the Microsoft. Silverlight. Testing name control.

The createtestpage () method creates a uielement, as shown in

Modify test. CS:

1 [testmethod]
2 Public void testmethod ()
3 {
4 assert. istrue (true );
5}

The running result is as follows:

It will tell you this assert (whether the assert is correct and how long it takes to operate the process)

If you change assert. istrue (true) to assert. istrue (false), the following interface appears:

At this point, you must wonder why I have to use unit tests?

It seems useless?

Let me talk about the following situation. You may know how useful unit testing is.

Imagine that you have just completed a piece of code today, but you want to test whether he works right?

You should test every situation and click every button to see if it works normally.

At this time, you will be very depressed, especially when a bug occurs.

You need to check each piece of code to find out what went wrong.

Sometimes you have to stay up all night, but there is a unit test.

You can write unit test code in a small amount of time to test the variation of each variable.

And you can run this unit test as soon as you leave the company.

The next day I checked several of them were green (that is, running as expected), and the others were red (not running as expected)

In this way, you can rest assured and work more efficiently.

An example is as follows:

1. First, add a Silverlight application called silverlightapp to the unittest4silverlight project.

And set the START project or the unittest4silverlight Project

2. The following code modifies the page. xaml ui:

1 <usercontrol X: class = "silverlightapp. Page"
2 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
3 xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml%22
4 width = "400" Height = "300">
5 <grid X: Name = "layoutroot" background = "# 3c3c3c">
6 <button X: name = "BTN" content = "unit test" horizontalalignment = "center" verticalignment = "center" fontsize = "20" padding = "20, 5" Click = "btn_click"/>
7 </GRID>
8 </usercontrol>

3. The code for modifying page. XAML. CS is as follows:

1 Public int clickednum = 0;
2
3 Public page ()
4 {
5 initializecomponent ();
6}
7
8 Public void btn_click (Object sender, routedeventargs E)
9 {
10 clickednum ++;
11}

Temporarily set all variables and methods to public

Recompile the entire project

4. Add a reference to the silverlightapp in the unittest4silverlight project as follows:

5. Modify the test. CS file in the unittest4silverlight project as follows:

1 using system;
2 using system. Collections. Generic;
3 using Microsoft. visualstudio. testtools. unittesting;
4 using silverlightapp;
5
6 namespace unittest4silverlight
7 {
8 [testclass]
9 public class test
10 {
11 private page = new page ();
12
13 private void testclick ()
14 {
15 page. btn_click (this, null );
16}
17
18 [testmethod]
19 public void testmethod ()
20 {
21 testclick ();
22 testclick ();
23 testclick ();
24 testclick ();
25 assert. areequal (page. clickednum, 3 );
26}
27}
28}

Because we have already clicked the mouse four times (call the testclick function to click)

Clickednum should have been increased to 4, but we assert clickednum = 3

There are obvious bugs, so the results after running are as follows:

You can see that the expected value is 4, but we passed it to 3, which is obviously incorrect. modify this bug and we will get the following running result:

Of course, the unit test in the actual project is not that simple.

For more functions of unit test, see SDK and Professional Software Testing books.

Conclusion:

This article shows how to use unit test in Silverlight to improve work efficiency.

Http://www.cnblogs.com/ibillguo/archive/2008/10/27/1320067.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.