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.


Initializing Core Data In Swift 4 For Mobile App Development

There are multiple ways to store data in your iOS app but one of the best way is to use core data . ...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

TFS to VSTS Migrations

The next evolution of TFSWhether you’re a startup or Enterprise or you’re a team of just...

Read More >

Microsoft Azure : Machine Learning - Price Prediction

We'll create linear regression model that predicts price of automobile based on different variab...

Read More >

DevOpSmartBoard - Azure Pipelines

DevOpSmartBoard -The Ultimate End-To-End and One Stop Dashboard solution for all the reporting needs...

Read More >

Using XMPP and Openfire server in ASP.NET C# - Part -1

Extensible Messaging and Presence Protocol (XMPP)The Extensible Messaging and Presence Protocol (XMP...

Read More >

Introduction to Azure DevOps Server (TFS) and Azure DevOps Services (VSTS)

In this blog we will talk about introduction to Azure DevOps Sever earlier called as Team Foundation...

Read More >

Configuring Azure DevOps Server Proxy

In this blog we will discuss about the Azure DevOps Proxy Server. Azure DevOps Server Proxy distribu...

Read More >

GitHub - Administration

To understand GitHub, you must first have an understanding of Git. Git is an open-source version con...

Read More >

Canarys Automations Pvt Ltd recognized as finalist for 2017 Microsoft DevOps Partner of the Year Award

  Bengaluru, Karnataka, India — June 2, 2017 — Canarys Automations Pvt Ltd tod...

Read More >


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

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *