Unit Test in C# (Xamarin Forms) - Blogs

Unit Test in C# (Xamarin Forms)

Unit Test are important for a good quality and maintainable code. It help to find error in our code in a very easy manner. For maintenance a software code it is a good practice to write a Unit test in your project.

Unit text is performed by the developer and not by the end-user.

There is some proper rule and guidelines which we need to followed while writing Unit test.

Best way to write Unit Test

  • Unit test should be independent.
  • Code coverage of testing code should be above 85%.
  • Unit test should be simple as there is no confusion of correctness of unit test code.
  • Execution of unit test should be fast and generate accurate result.
  • Should cover one condition of method at a time.
  • There should be separate project of writing Unit test.
  • Parent class should be tested first then the child class.
  • Unit test project should be maintainable and well organized.

We have 2 frameworks for writing Test cases in C#

  • MS Test
  • NUnit

For writing test cases we have to follow 3 simple pattern.

  • Arrange :- it is use for all the necessary precondition and inputs.
  • Act :-  use for Object and Method for the test.
  • Assert :- here Expected result will be occurred.

Now, I am going to show you how to add Unit Test project in your project and how to write and test Unit test.



(Create Unit Test Project)


For creating Unit test we need to write click on project in solution explorer.

Follow my following image.


After that select new project and give a meaning full name for your Unit Test project.

As I have given in following image.


Now your Unit Test project successfully created and added in your solution.



(Create Method)

As I am explaining how to create Unit Test  and how to test our method which we have written for our application or project so here I am taking a simple example.


So I have already created my method class for my Xamarin Forms calculator app. You can see it in following image.


In this image you can see it I have created a method class in my PCL project  and I have written  method with the name of “Addition” and “Mul”.


Now I am going to test my “Addition ” Method in Unit Test.


For that I have to add references of main PCL project in my Unit Test project. After that I am able to use that method class while creating that instance of the class.


(Create Unit Test)


In the following screen you can see unit test code which I wrote.


Here you can see it in Arrange part I have created instance of Method Class which I have written in PCL project. After that I am able to access of “Addition” Method and its property.


ACT in this portion I am passing the value in method parameter.


Assert in this part you can see I am comparing value.


Once our Unit Test case is done we can check while executing of our Unit test.


(Executing Unit Test)


For executing this test case, just follow this screen.


Here, I right clicked on method and selected the option “Run selected test”.

Now my test got successful.


Hope this will help you to get started with Unit Test.






As a software developer Writing Unit Test case is very good practice. And it is very helpful for software development.

If Unit Test is written and performed properly, then consistently developed software will be lot more effective at delivering the solution in a predictable and managed way.


Populate data as Facebook feeds in ASP.NET

Want to load the data dynamically as Facebook feeds?Need to populate the data when scrollbar comes d...

Read More >

Partnership Program

The Canarys Partner Program:Canarys is all set to expand its business reach across the Globe and is ...

Read More >

Material Design in Android – Working with Toolbar

Introduction to Material DesignMaterial Design is a comprehensive guide for visual, motion and inter...

Read More >

How to make your software demos effective.

 I have been involved in Software demonstrations for the last 15+ years. I have done a lot of s...

Read More >

Azure App Service Scaling Features And Capabilities

How to scale the application in Azure App Service. There are two workflows for scaling, scale up and...

Read More >

Brief Journey to Asp.Net MVC Framework

Introduction: ASP.NET MVC is a  framework for building web applications that uses the mode...

Read More >

What's New in Microsoft Dynamics NAV 2017

We were hearing about Dynamics NAV 2017 since long time that it will be releasing by the end of 2016...

Read More >

Time to Rock. Time for Our TechEd 2012 Rockstar.

Our Senior ALM specialist Sanjeev Kumar was awarded TechEd Rockstar in the Microsoft TechEd (March 2...

Read More >

How to implement and use Extension Methods in C#

Do you wish to extend the built-in types with new methods?Want to extend custom types with new metho...

Read More >


The k8s node affinity feature is to ensure pods are hosted on a particular node. As mentioned in pre...

Read More >


Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *