Wednesday, February 19, 2014

Unit testing in Windows Store App

In Visual Studio 2012 onwards , there is a new project template called Unit Test Library (Windows Store apps). A Unit Test Library can contain many test classes.Test classes are marked with the TestClass attribute. Test classes contain test methods, initialization code, and cleanup code.

Test method — Test methods are marked with the TestMethod attribute. These methods run
separately one by one. Each method has a result that determines whether the test was successful,
or if it failed.

Initialization — Every test class can have a single method decorated with the TestInitialize
attribute. This method runs before every test method to perform any intitialization needed. Unit
tests should not use real data, network connections, or database access. If a component needs
a data source, it is best to create a fake one. You can do this in the initialization stage so that
every test method can use it.

Cleanup — Every test class can have a single method decorated with the TestCleanup attribute.
This method runs after every test method to clean up any changes after the test methods.
You can ensure that every test method runs on the same environment and configuration.


This unit test is running on user defined data set and it running with application build state. Then no need to run the application to test it. just Build is enough :)

Lets start.,

    1. Create new Windows store app. (name it as UnitTestAdd)
      2. Add this class to it (add-> new item->class)


      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;

      /*
      * This code is owned to Prabath Maduranga Peiris
      * Using for demonstration purposes in
      *
      * www.prabathsl.blogspot.com
      *
      */

      namespace Simple_Addition
      {
      public class cal
      {
      public int add(int a, int b)
      {
      return a + b;
      }
      }
      }

      ** Actually we are gonna test this add() code unit.

      3.  Then R-click on the Solution and Add-> new Project -> select Unit Test Library and add name to it.

      4. Then go to the Unit test  project and R-Click on the References from Reference Manager select Solution -> Project and select your project 



      5. Then create property for unitTest class , It should be type of object that you gonna test


      Simple_Additioncal testObject; 

      6. Then initialize the unit test .


      namespace testAdditionDemo
      {
      [TestClass]
      public class UnitTest1
      {
      Simple_Additioncal testObject;

      [TestInitialize]
      public void Initialize()
      {
      testObject = new Simple_Additioncal();
      }

      7. Then Create Test Method


        [TestMethod]
      public void TestMethodAddition()
      {
      int a = 34;
      int b = 20;
      int expectedAns = 54;
      int answer = testObject.add(a,b);

      /*
      * Assert.AreEqual(expectedResult, cakculatedAnswerFromUnit,error Message )
      */


      Assert.AreEqual(expectedAns,answer,String.Format("Expected Result for {0} + {1} = {2}",a,b,expectedAns));
      }

      8. Then build the solution

      If you can't see the Test Explorer then go to Test -> Windows -> Test Explorer

      Here is the results

      1. I change add method in Cal  class in to this

       public int add(int a, int b)
      {
      return a - b;
      }

      then our test should be false ...

















      result with default Cal class..




















      Enjoy :)




      Full Code In Media Fire 
      http://bit.ly/1blHlHA

      0 comments: