Unit Testing in .NET Framework
What is unit testing?
- Unit testing is the testing of an individual unit or group of related units. It falls under the class of white box testing.
- Unit tests give developers and testers a quick way to check for logic errors in the methods of classes in C#, Visual Basic .NET, and C++ projects.
- Performed by the developer, prior to delivering code to testers.
- A unit test can be created one time and run every time that source code is changed to make sure that no bugs are introduced.
Why unit testing?
1. It is part of Agile methodology.
2. Gives you confidence in changing/maintaining code.
3. High quality code.
4. TDD helps you to realize when to stop coding.

I'm Safe
We want proof that our classes work right.,
1. If we had a bug, we want proof it won’t happen again.
2. If we change the code, we want proof we did not break anything.
3. If we do break something, we want to know about it as quickly as possible.
4. If we do break something, we want to know what exactly is broken: a failing test is the ultimate form of bug report.
Traditional Testing
-
Test the system as whole.
-
Errors go undetected.
-
Individual components are rarely tested.
-
Isolation of errors and difficult to track.
How?
Difference between Unit testing and others
1. Performed on the class (“unit”) level
2. Each unit is tested in isolation
3. Automatic
4. Easy to write
5. Scope is smaller, easier to fix errors
How to do in .NET ?
Two ways:
- Using 3rd party tools like NUnit, xUnit, Resharper, etc.,
- In-built Unit testing (introduced in VS2012)
3
rd Party Tools
NUnit
- NUnit is a unit testing framework for performing unit testing based on the .NET platform.
-
NUnit is not an automated GUI testing tool.
- It is not a scripting language, all tests are written in .NET supported languages, e.g., C#, VC, VB.NET, J#, etc.

Resharper
- Analyze code quality in C#, VB.NET, XAML, ASP.NET, ASP.NET MVC, JavaScript, CSS, HTML, and XML.
- Automated solution-wide code refactorings to safely change your code base.
- Intellisense
- Instantly traverse your entire solution
xUnit
-
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework.
-
It is an upgrade of NUnit.

Unit Test Driven
-
TestDriven.Net makes it easy to run unit tests with a single click, anywhere in your Visual Studio solutions.
-
Has to be purchased for enterprise
In VS2012
Why in VS2012?
In VS2010, they faced the following issues:
-
MS-Test too slow
- Bad C/C++ support
Moving to VS 2012...
sample:
// unit test code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BankTests
{
[TestClass]
public class BankAccountTests
{
[TestMethod]
public void TestMethod1()
{
}
}
} Demos

UNIT TESTING IN .NET
By praveen_jegan
UNIT TESTING IN .NET
- 278

