Getting Started with Unit Testing in Silverlight for WP7
published on: 3/1/2011 | Views: N/A | Tags: Testing
by WindowsPhoneGeek
In this article I am going to talk about Unit Testing in Silverlight for Windows Phone 7.
What is Unit Testing?
Unit testing is a method by which individual units(usually methods, properties etc.) of source code are tested to determine if they are fit for use. Ideally, each test case is independent from the others.
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.
Why using Unit Tests:
- Unit testing allows the programmer to reactor code at a later date, and make sure the module still works correctly
- By testing the parts of a program first and then testing the sum of its parts, integration testing becomes much easier.
- When software is developed using a test-driven approach, the unit test may take the place of formal design.Each unit test can be seen as a design element specifying classes, methods, and observable behavior
Getting Started with Unit Testing in Silverlight for WP7
The first thing we have to mention here is that there is no option for a Windows Phone 7 test project within the IDE. However in order to workaround this you can choose between the following approaches:
Option1) To use the WP7 version of the Silverlight Unit Test Framework created by Jeff Wilcox's::
- Updated Silverlight Unit Test Framework bits for Windows Phone and Silverlight 3
- Jeff Wilcox's session on Unit Testing Silverlight & Windows Phone Applications from MIX10. ( Using a Silverlight-based unit test framework)
Option2) Use the NUnit Windows Phone 7 FREE framework
Option3) Use another third party framework. You can find lots of free implementation in CodePlex.
This article is focused on how to implement a sample unit testing environment in a Windows Phone 7 using the Silverlight Unit Test Framework .
Now lets get started with the unit testing. Here are the steps:
1. Download the Silverlight Unit Test Framework for Windows Phone 7 from Jeff Wilcox's blog or get the dlls attached at the end of this article. Create a sample forlder called dlls and paste the Microsoft.Silverlight.Testing.dll and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll assemblies there.
2.Create a sample Windows Phone 7 application project and add reference to the following assemblies
Microsoft.Silverlight.Testing.dll
Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll
NOTE: You will get a warning about adding reference to Silverlight 3 assemblies, click yes when asked if you want to continue.
3.In our case we will test the WatermarkedTextBox control created in my previous article: WP7 WatermarkedTextBox custom control.
So we will add reference to WatermarkedTextBoxControl.dll. Alternatively you can create another project and reference it in the test project.
4.Create a sample class SampleTests.cs and add the following code in to it:
[TestClass]
public class SampleTests : SilverlightTest
{
[TestMethod]
public void AlwaysTrue()
{
Assert.IsTrue(true, "this method always pass");
}
[TestMethod]
[Description("Create WatermarkTextBox control")]
public void CreateWatermarkTextBox()
{
WatermarkedTextBox textBox = new WatermarkedTextBox();
Assert.IsNotNull(textBox);
}
[TestMethod]
[Description("Basic WatermarkTextBox control properties default values")]
public void WatermarkTextBoxPropertiesDefautValue()
{
WatermarkedTextBox textBox = new WatermarkedTextBox();
Assert.IsNotNull(textBox);
Assert.AreEqual(textBox.Text, "");
Assert.AreEqual(textBox.Watermark, "");
Assert.IsNull(textBox.WatermarkStyle);
}
}
5.Go to MainPage.xaml and add a sample button which will trigger the test. Add the following code into its click handler:
<Button Content="Start Tests" Click="Button_Click"/>
private void Button_Click(object sender, RoutedEventArgs e)
{
var testPage = UnitTestSystem.CreateTestPage();
IMobileTestPage imobileTPage = testPage as IMobileTestPage;
BackKeyPress += (s, arg) =>
{
bool navigateBackSuccessfull = imobileTPage.NavigateBack();
arg.Cancel = navigateBackSuccessfull;
};
(Application.Current.RootVisual as PhoneApplicationFrame).Content = testPage;
}
NOTE: We have to handle the Back button so that you will be able to navigate to the previous test method while browsing through the test results.
6.Build and run the project. Here is the result:
Note that you will need to wait a few seconds until the tests run.
If any particular test fails you will get an alert popup message that explains more info about the problem. Just press F5 and continue with the rest of the tests.
Finally you will get a full report about the passed and failed tests.
7. That`s it. Now you have a working UnitTesting project in WP7.
That was all about getting started with Unit Testing in Silverlight for Windows Phone 7. You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Mr
posted by: Ravindran on 4/29/2011 10:09:11 PM
Nice one, thanks.
Ravi.antone@gmail.com
Mocking on Mango
posted by: John Patt on 11/1/2011 1:01:47 PM
What automatic mocking solutions have people found or is manual mocking the current order of the day?
I have tried several solutions Moq, Rhino, JustTelerik Free Edition and none seem to be too friendly with SL4 and Mango
There are some claiming success with RC using old versions of MOQ but could not replicate their success with RTM
I have been using 7.1 projects with Windows Phone Essentials and both Nunit and VSTS and still coming across reflection errors
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
