Unit Test in C# (Xamarin Forms) - Blogs
X
12Mar

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.

 

STEP 1

(Create Unit Test Project)

 

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

Follow my following image.

1

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

As I have given in following image.

2

Now your Unit Test project 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 following image.

methiod

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.

STEP 3

(Create Unit Test)

 

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

Capture

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.

STEP 4

(Executing Unit Test)

 

For executing this test case, just follow this screen.

4

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.

 

 

 

Conclusion

 

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.

Related

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

VSTS RollUp

VSTS RollUp is a web service which provides summed values of select fields for all child work items ...

Read More >

Ansible Ad-Hoc Commands and Playbooks

An ansible ad-hoc command is a one-line command that lets you perform basic tasks efficiently withou...

Read More >

What is Selenium Webdriver

Automated software testing is a process in which software tools execute pre-scripted tests on a soft...

Read More >

How to Save Photos to Custom Album in iPhone/iPad Photo Library

Here is a way through which you can create your custom Album and can save captured images within tha...

Read More >

Mobile Operating Systems And Cross Platforms

Current Popular mobile operating systems are:Android from Google Inc.(Open source)initial release :2...

Read More >

Create custom project templates in Visual Studio

Visual studio installation comes with the various predefined project templates, and we can use one o...

Read More >

Bugzilla to TFS Migrator (Part 2 of 4) – Installation

This is the second part of the Bugzilla to TFS Migrator blog series covering the installation of the...

Read More >

Microsoft Azure : Mobile Services - HTML with JavaScript

 OverviewWe will add cloud-based backend service to HTML app using Azure Mobile Services. We wi...

Read More >

Partnership Program

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

Read More >

Share

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

Sign Up

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *