Unit Test In C# With Xamarin.Forms

In this article, we will discuss how to write unit test in C# with Xamarin.Forms.

The unit test is important for good quality and maintainable code. It helps to find an error in our code in a very easy manner. For the maintenance of a software code, it is a good practice to write a Unit test in your project.

This is not for users working on software but for developers who have developed that software.

There are some proper rules and guidelines which we need to follow for writing Unit tests.

Best way to write Unit Test

  • The unit test should be independent.
  • Code coverage of testing code should be above 85%.
  • The 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 an accurate result.
  • Should cover one condition of a method at a time.
  • There should be a separate project of writing Unit test.
  • The 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 patterns.

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

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

STEP 1 - (Create Unit Test Project)

For creating Unit test we need to right-click on the project in solution explorer.

Follow my following image.

Xamarin

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

As I have given in the following image.

Xamarin

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

STEP 2 - (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 the following image.

Xamarin

  1. namespace DemoTest  
  2. {  
  3.     public class Methods  
  4.     {  
  5.         public int Addititon(int x, int y)  
  6.         {  
  7.             return x + y;  
  8.         }  
  9.         public int Multi(int x, int y)  
  10.         {  
  11.             return x * y;  
  12.         }  
  13.     }  
  14. }  

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 from the 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.

STEP 3 - (Create Unit Test)

In the following screen, you can see my unit test code which I have written.

Xamarin

  1. namespace UnitTestProject1  
  2. {  
  3.     [TestClass]  
  4.     public class UnitTest1  
  5.     {  
  6.         [TestMethod]  
  7.         public void TestMethod1()  
  8.         {  
  9.             //Arrange  
  10.             Method m1 = new Method();  
  11.             int expectedResult = 16;  
  12.   
  13.   
  14.             //Act  
  15.             int actualResult = m1.Addition(11, 5);  
  16.   
  17.   
  18.             //Assert  
  19.             Assert.AreEqual(expectedResult, actualResult);  
  20.               
  21.         }  
  22.     }  
  23. }  

Here, you can see it in Arrange part I have created an 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 values.

Now, our Unit Test case is done and we can check while executing our Unit test.

STEP 4 - (Execute Unit Test)

For execution, this test case just follows the following screen.

Xamarin

Here, you can see I have to right-click on the method and select the option “Run selected test”.

Now, my test was successful.

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

Conclusion

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

If Unit Test is written and performed properly and consistently software are a lot more effective at delivering the correct solution in a predictable and managed way.