Unit Testing in C#

Testing any product system becomes necessary to verify its usability and validate its intended purpose. If this testing procedure can be executed on every modification of the product system as a sanity check, it becomes a measure to alleviate chances of any discrepancy introduced.

Thus, Unit testing in software development plays a important role to verify intend and pacify chances of bug instigation on the very basic unit of software. Unit testing is a level of software testing where individual units of software are tested against logical flaws. In object oriented programming, the smallest unit is usually a ‘method‘.

How to start writing Unit Tests

  • Identify logical unit, usually a method or a property which needs to be tested.
  • Create setup in the unit test class to invoke that method.
  • Initiate the call/trigger to the method in focus from your test method.
  • And the last step, compare the actual results/outcome of the call to the expected result/outcome.

We can start with a simple implementation of a calculator and its unit tests.

Lets take basic methods of a calculator, Add and Subtract. I will show you how we can create multiple tests cases on these simple methods.

public class Calculator
{
public Calculator() {}
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double minuend, double subtrahend)
{
return minuend – subtrahend;
}
}
view raw Calculator.cs hosted with ❤ by GitHub

Now, we will see the unit tests corresponding to these two methods.

[TestMethod]
public void Test_Add_WholeNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double sum = calculator.Add(3, 5);
// Assert
Assert.AreEqual(8, sum);
}
[TestMethod]
public void Test_Add_FractionalNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double sum = calculator.Add(3.7, 5.3);
// Assert
Assert.AreEqual(9, sum);
}
[TestMethod]
public void Test_Add_NegativeNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double sum = calculator.Add(-5.6, 10.6);
// Assert
Assert.AreEqual(5, sum);
}
[TestMethod]
public void Test_Subtract_WholeNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double difference = calculator.Subtract(5, 3);
// Assert
Assert.AreEqual(2, difference);
}
[TestMethod]
public void Test_Subtract_FractionalNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double difference = calculator.Subtract(5.3, 2.3);
// Assert
Assert.AreEqual(3, difference);
}
[TestMethod]
public void Test_Subtract_MinuedNegative()
{
// Arrange
var calculator = new Calculator();
// Act
double difference = calculator.Subtract(-5.3, 2.3);
// Assert
Assert.AreEqual(-7.6, difference);
}
[TestMethod]
public void Test_Subtract_BothNegativeNumbers()
{
// Arrange
var calculator = new Calculator();
// Act
double difference = calculator.Subtract(-5.3, -2.3);
// Assert
Assert.AreEqual(-3.0, difference);
}
[TestMethod]
public void Test_Subtract_MinuedLessThanSubtrahend()
{
// Arrange
var calculator = new Calculator();
// Act
double difference = calculator.Subtract(5.3, 7.3);
// Assert
Assert.AreEqual(-2, difference);
}

As you can see, these test cases are logical scenario based, it is not enough to test the method with only positive scenario as our implementation usually breaks for negative and borderline scenario, so it becomes necessary for a effective unit test to verify these corner cases.

Also, as a better design practice, try not to club two or more ‘Assert‘ in one test method, one assertion in one method makes easier to identify and pin-point the failure scenario when tests fail.

Further, to increase readability few things should be kept in mind, first, unit test method name should preferably include name of method under test, scenario and expected result; second, follow AAA pattern i.e. divide your test method in Arrange-Act-Assert sections.

Arrange – creating instances, Act – invoking the method under focus and Assert – comparing expected and actual results.

Here unit tests are written using one of the most popular and intuitive unit testing framework in .NET – MsTest. NUnit is also a good option but MsTest is preferred because of its seamless integration with Visual Studio IDE.

Find complete project here.

Published by Code Pantheon

I am a learner and a believer.

Join the Conversation

  1. Unknown's avatar
  2. Unknown's avatar

2 Comments

Leave a comment

Design a site like this with WordPress.com
Get started