Category Archives: Unit Test

Unit Testing Succinctly

“The illiterate of the 21st century will not be those who cannot read and write, but those who cannot learn, unlearn and relearn” Alvin Toffler.

My 2013’s motto is LEARNING, learn how to be a better human, learn about life, science, cultures and of course as a developer learn as much technical things as I can. And to be honest, learning for free is not that bad and that is why I keep an eye on my twitter feed to be among the first to get discounts or free goodies.

One of the cool FREE resources is the Syncfusion eBooks. Wait! You didn’t hear about them and you want to know how I did? But first you want to know what Syncfusion is, right? Okay, okay…

Syncfusion & the Succinctly eBooks

Syncfusion is a 12-year-old company providing a range of enterprise-class software components and tools for .Net platform.

And since I’m a user of their Metro Studio, I keep getting notifications with all the discounts and free goodies. You may have seen some tweets about the FREE eBooks, the Succinctly eBooks.

Each one of these is about a different technology and programming language (Azure, TypeScript, WPF etc.).

Unit Testing Succinctly

The book I am reading right now is the Unit Testing Succinctly.

In a nutshell, this 128 pages book will help you understand and use unit tests in your projects.

unit test cover

 

 

The eBook covers exactly 12 chapters

  • Why Unit Test?
  • What is Unit Testing?
  • Proving Correctness
  • Strategies for Implementing Unit Tests
  • Look before You Leap: The Cost of Unit Testing
  • How Does Unit Testing Work?
  • Common Unit Test Tools
  • Testing Basics
  • Unit Testing with Visual Studio
  • Unit Testing with NUnit
  • Advanced Unit Testing
  • Unit Testing for Other Purposes

Why Unit Test?

If you are going to make the effort to learn and, spend time to implement unit tests, you should understand why you are doing it. It does make sense, right? ;)

By reading the Unit Testing Succinctly eBook, you will understand that unit tests help measuring correctness, maintaining working code and covering all code execution paths.

What is Unit Testing?

You may have heard about unit tests (especially that one tweet “If you are not using Unit tests, you are doing it wrong”) but you have no idea what is it really or how to do it.

This chapter is the answer for you, it explains in details and precise words what is a Unit, a Test and what is unit testing?

Proving Correctness

As mentioned for Chapter one, one of the reasons of unit testing is to prove correctness.

This chapter covers in details the meaning of proving correctness from a developer point of view and how to do it exactly from verifying the contract to verifying data transformation results.

Strategies for Implementing Unit Tests

I bet you have many questions in your head right now especially if you are not used to unit testing. Is it possible to use it while I’m already done with the application and I’m in the maintenance phase? Should I write my tests before coding?

Well, all the answers you need are in this chapter.

Look before You Leap: The Cost of Unit Testing

This chapter is a formalized look at the cost and benefits of unit testing.

How Does Unit Testing Work?

If we are investing time to understand and implement Unit tests, it is because we want to use them. This Chapter explains how you can use Unit tests and how they work. It contains code samples to better explain how to load assemblies, find methods and invoke them.

Common Unit Test Tools

In this chapter, things get more technical. You can find some of the most used and known unit test tools like NUnit and Visual Studio Unit Test.

Testing Basics

A brief chapter in which the author guides you on the unit testing process, how to deal with bugs, report and track them.

That is not everything

win

 

 

Syncfusion is being very generous, other than the free eBooks, 3 lucky readers will win WINDOWS PHONE essentials package. All you have to do is to comment on this post and leave an email address for Syncfusion to contact you.

Optionally, you can follow me on twitter ;)

Mango Baby Steps : Unit Testing your app

Making apps is good , making apps that work perfectly is better Winking smile 

In order to make sure that your app works fine you may want to test your code .

So let’s take a walk through Mango Unit Testing Framework.

First thing you need to know is that we are using Silverlight Unit Test Framework by Jeff Wilcox who recently updated the Silverlight Unit Test framework Binaries to be compatible with Mango.

You can grab the new assemblies from here.

Next you should have something to test …right ? Smile with tongue out 

Nothing complicated, we will work with the famous “ Hello Mango” sample.

In Visual Studio : File—> New project –> Silverlight for Windows Phone

Choose Windows Phone application and name it “ Hello Mango” . Since we are working with Mango, don’t forget to choose the Windows Phone 7.1 version Winking smile 

new project

 

 

We will change the application title to “ Hello Mango” , the page title to “ Unit Test” and we will add a textblock containing “ Hello Mango” :

Code Snippet
  1. <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
  2.             <TextBlock x:Name="ApplicationTitle" Text="Hello Mango" Style="{StaticResource PhoneTextNormalStyle}"/>
  3.             <TextBlock x:Name="PageTitle" Text="Unit Test" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
  4.         </StackPanel>
  5.  
  6.         <!–ContentPanel – place additional content here–>
  7.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  8.             <TextBlock Height="146" HorizontalAlignment="Left" Margin="92,137,0,0" x:Name="txt" x:FieldModifier="public" Text="Hello Mango" VerticalAlignment="Top" Width="277" />
  9.         </Grid>

 

Now that you have your sample app ready , run it to make sure that everything is fine and then we will move to the test project.

the test project is also a Windows phone application called “ HelloMangoTest”

test

 

 

We need to convert the windows phone application to a Unit Test project:

Download the assemblies as describe it below , unzip the file and make sure they are unlocked so you can use them on your local machine.

Add the two assemblies to the “ HelloMangoTest” project :

addReference

                                  ref

 

You have also to delete the MainPage.xaml  because Silverlight Unit Test Framework has a TestPage.

delete

 

One more thing to do :

In the App.xaml.cs, add

Code Snippet
  1. this.RootVisual = UnitTestSystem.CreateTestPage();

  , you will have this code:

Code Snippet
  1. public App()
  2.         {
  3.             
  4.             UnhandledException += Application_UnhandledException;
  5.             InitializeComponent();
  6.             this.RootVisual = UnitTestSystem.CreateTestPage();

 

Set the test project as a Startup Project and now you are ready to write the tests.

add a new class and name it “ test” :

newClasstestt

 

In this test we want to make sure that when the MainPage is initialized , there is indeed  a textBlock ( we called it “ txt” ) . Here is the code :

Code Snippet
  1. using Microsoft.Silverlight.Testing;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using HelloMango;
  4.  
  5.  
  6. namespace HelloMangoTest
  7. {
  8.     [TestClass]
  9.     public class test
  10.     {
  11.         [TestMethod]
  12.         public void pass()
  13.         {
  14.             MainPage page = new MainPage();
  15.             Assert.IsTrue(page.txt != null);
  16.         }
  17.         
  18.  
  19.     }
  20. }

 

When you run the project, you will get this first screen :

first screen

And I have no idea what this screen is Smile with tongue out   I need to ask Jeff Wilcox about it.

Surprisingly it’s not working Confused smile So I did a small search and found out that I shouldn’t delete the MainPage and change the Rootvisual in the App.xaml.cs.

Instead I put this code in my MainPage.xaml.cs in the test project :

Code Snippet
  1.  
  2. using Microsoft.Silverlight.Testing;
  3.  
  4. namespace HelloMangoTest1
  5. {
  6.     public partial class MainPage : PhoneApplicationPage
  7.     {
  8.         
  9.         public MainPage()
  10.         {
  11.             InitializeComponent();
  12.             const bool runUnitTests = true;
  13.             if (runUnitTests)
  14.             {
  15.                 Content = UnitTestSystem.CreateTestPage();
  16.                 IMobileTestPage imtp = Content as IMobileTestPage;
  17.                 if (imtp != null) { BackKeyPress += (x, xe) => xe.Cancel = imtp.NavigateBack(); }
  18.             }
  19.         }
  20.     }
  21. }

 

If the Boolean is true, the unit test become the content of the Mainpage.

Let’s run again …Wohoo! It’s working not only on the emulator but also  on a real device :

150_0168150_0170

The list of Test classes under your project will be shown in first page. You can go into each class to see all test methods by clicking on the name of class.

In our case you will see Test and pass. Click on the method’s name ( pass) and you will get the test information , the result is “ passed”.

That was an introduction of how to use Silverlight Unit Test Framework with Mango Smile

Follow

Get every new post delivered to your Inbox.