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

Twitter Bootstrap – An UI Framework for Web Applications

Twitter Bootstrap is an open source front end framework for HTML, CSS and JS, using which we can cre...

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 >

Introduction to KnockOut.Js in Asp.Net

What is KnockOut.Js?Knockout.Js (simply KO) is a powerful JavaScript library which allows developers...

Read More >

Introduction to Load Testing

 Well, before learning what is Load testing. Let us discuss briefly the performance testing.&nb...

Read More >

VSTS Team project Monitor

Let us discuss how to install VSTS team project monitor and how we can make use of it easily.Particu...

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 >

WCF SOAP and REST Services

WCF (Windows Communication Foundation) is a programming platform and runtime system for building, co...

Read More >

Maven’s Project Object Model

Maven’s POM.xml – A file to build ProjectsIn the previous article i.e. ‘Apache Maven – A tool to...

Read More >

What’s new: Extension for Microsoft Dynamics NAV 2016

We all have seen the situation where we need to put in lot of efforts to modify the standard NAV obj...

Read More >

Share

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

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *