Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Write Unit Tests in C#

Similar presentations


Presentation on theme: "How to Write Unit Tests in C#"— Presentation transcript:

1 How to Write Unit Tests in C#
Dhananjay Kumar Infragistics Consultant Microsoft MVP, C# Corner MVP

2 Some Goodies Two active participants among you may win this cool goodies . We will announce winner name in webinar recap blog post which will be published on Infragistics blog Recording of the webinar with all resources will be available on webinar recap blog post We will ask you for your mailing address and courier you the goodies in India

3 I am Dhananjay Kumar Infragistics Consultant 6 times Microsoft MVP
7 times C-SharpCorner MVP 850+ Blog posts , 70+ talks, and 50 + webinars 7+ years of development and evangelism experience @debug_mode Blog :

4 Agenda About Unit Test Write Unit Test using MS Test
Write Unit Test using NUnit Testing Private Methods Test Set Up and Tear Down Calculator App using TDD

5 What is Unit Test Unit Test is a piece of code which tests behaviour of a function or class. Unit Tests are written by the developers.

6 ? What is Unit Test Add function Options
Input (num1,num2) Sum(num1,num2) ? Options Use function in application and then test it with the application Write code to test the function before it goes to production. This code is called Unit Test

7 Why Unit Test ? Unit Testing reduces the level of bugs in production code.  Unit Testing saves you development time.  Unit Testing saves time in debugging later Automated Unit tests can be run as frequently as required with different set of input A good Unit Tests are a form of documentation Unit Testing forces you to confront the problem head on Unit Tests allows you to make big changes to code quickly Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you'd expect from that.

8 Unit Test in .NET Unit Test in .NET MS Test NUnit

9 Structure of test project
SUT project:Calculator class:Calculator method : Add method:Mul Test : Calculator.Tests class:CalculatorTest method: ShouldAddNumbers method:ShouldMulNumbers

10 AAA Arrange Act Assert

11 Write Test using MSTest : Demo
Add reference of VSTS package in the test project or create a test project by selecting Unit Test project template Add reference of the SUT project Create Test class and Test methods Execute test using Visual Studio Test Explorer

12 Write Test using Nunit : Demo
Add reference of NUnit package in the test project Add reference of the SUT project Create Test class and Test methods Execute test using either of three options. Visual Studio Test Explorer NUnit UI NUnit command prompt

13 Test SetUp and TearDown
Gets executed before execution of first test Test SetUp Gets executed after execution of all the tests. Test Tear Down Create a database connection before execution of first test Create instance of a particular object before execution of first test Delete al connection to data base after execution of all the tests Delete particular file from the system before execution of any test

14 Test SetUp and TearDown
MS Test SetUp : [ClassInitalize] TearDown : [ClassCleanup] NUnit SetUp : [TestFixtureSetUp] TearDown : [TestFixtureTearDown]

15 Test First Approach or TDD
Red Green Refactor Write a failing test (run the test(s) – they should be RED) Make it pass in the simplest way possible (tests are GREEN) Now clean up the code (eliminate duplication and other code smells) (REFACTOR)

16 Summary What is Unit Test Why Unit Test Write Test using MSTest
Write Test using NUnit Test Fixture SetUp and Test Fixture Tear Down Test SetUp and Test TearDown Test First Approach Testing Private methods

17 What Infragistics can offer you?
We welcome all of you to take advantage of a FREE 30 Day Trial by downloading the product at: Please reach out to us at for any follow up questions you may have. We welcome the opportunity to assist you.


Download ppt "How to Write Unit Tests in C#"

Similar presentations


Ads by Google