Canarys | IT Services


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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.