ANGULAR.jS - Infragistics

How to Write Unit Tests
in C#
Dhananjay Kumar
Infragistics Consultant
Microsoft MVP, C# Corner MVP
http://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 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 : 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
Sum(num1,num2)
?
Options
Input (num1,num2)
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
Test :
Calculator.Tests
class:Calculator
class:CalculatorTest
method : Add
method: ShouldAddNumbers
method:Mul
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 project
Create 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 project
Create Test class and Test methods
Execute test using either of three
options.
1. Visual Studio Test Explorer
2. NUnit UI
3. NUnit command prompt
Test SetUp and TearDown
Test SetUp
Test Tear Down
• Gets executed
before
execution of
first test
• Gets executed
after execution
of all the tests.
 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
NUnit
SetUp : [ClassInitalize]
SetUp :
[TestFixtureSetUp]
TearDown :
[ClassCleanup]
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.
2.
3.
4.
5.
6.
7.
8.
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
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.