How to Write Unit Tests in C# Dhananjay Kumar Infragistics Consultant Microsoft MVP, C# Corner MVP .
-
Upload
kerry-nichols -
Category
Documents
-
view
234 -
download
4
Transcript of How to Write Unit Tests in C# Dhananjay Kumar Infragistics Consultant Microsoft MVP, C# Corner MVP .
How to Write Unit Tests in C#
Dhananjay KumarInfragistics Consultant Microsoft MVP, C# Corner MVPhttp://debugmode.net
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
I am Dhananjay Kumar
Infragistics Consultant6 times Microsoft MVP 7 times C-SharpCorner MVP850+ Blog posts , 70+ talks, and 50 + webinars 7+ years of development and evangelism experience @debug_modeBlog : http://debugmode.net Email : [email protected]
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
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.
What is Unit Test
Add function Input (num1,num2) Sum(num1,num2)
?
Opti
ons
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
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.
Unit Test in .NET
Unit Test in .NET
MS Test NUnit
Structure of test project SUT
project:Calculator
class:Calculator
method : Add
method:Mul
Test : Calculator.Tests
class:CalculatorTest
method: ShouldAddNumbers
method:ShouldMulNumbers
AAA
Arrange Act Assert
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 projectCreate Test class and Test methods Execute test using Visual Studio Test
Explorer
Write Test using Nunit : Demo
Add reference of NUnit package in the test project
Add reference of the SUT projectCreate Test class and Test methods Execute test using either of three
options.1. Visual Studio Test Explorer2. NUnit UI 3. NUnit command prompt
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
Test SetUp and TearDown
MS Test
SetUp : [ClassInitalize]
TearDown : [ClassCleanup]
NUnit
SetUp : [TestFixtureSetUp]
TearDown : [TestFixtureTearDown]
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)
Summary 1. What is Unit Test2. Why Unit Test 3. Write Test using MSTest 4. Write Test using NUnit5. Test Fixture SetUp and Test Fixture Tear Down6. Test SetUp and Test TearDown 7. Test First Approach 8. Testing Private methods
What Infragistics can offer you?• We welcome all of you to take advantage of a FREE 30 Day Trial by downloading
the product at: http://www.infragistics.com/products/ultimate/download
• Please reach out to us at [email protected] for any follow up questions you may have. We welcome the opportunity to assist you.