Shawn Ng'iela

Writing Unit Tests in C#

Writing Unit Tests in C#

Unit testing is a crucial part of software development that helps ensure your code works as expected. In C#, the most commonly used framework for unit testing is MSTest, but there are also other popular frameworks like NUnit and xUnit. This presentation will cover the basics of writing unit tests in C# using MSTest.

Table of Contents

  • What is Unit Testing?
  • Setting Up Your Environment
  • Creating a Simple C# Class
  • Writing Unit Tests
  • Running Your Tests
  • Best Practices
  • Conclusion

What is Unit Testing?

Unit testing is the process of testing individual components of your software to ensure they work as intended. A unit test typically tests a single function or method in isolation.

Benefits of Unit Testing

Catches Bugs Early: Identify issues before they reach production. Facilitates Refactoring: Confidence to change code without breaking functionality.

Documentation: Tests serve as documentation for how the code is supposed to work.

Setting Up Your Environment

To get started with unit testing in C#, you need to set up your development environment:

Install Visual Studio: Ensure you have Visual Studio installed (Community edition is free).

Create a New Project: Create a new C# project (e.g., Console App).

Add a Test Project: Right-click on your solution, select “Add” > “New Project”, and choose “MSTest Test Project”.

Creating a Simple C# Class:

Let’s create a simple class that we will test. This class will contain a method to add two numbers.

1
2
3
4
5
6
7
8
// Calculator.cs
public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

Writing Unit Tests

Now, let’s write a unit test for the Add method in the Calculator class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// CalculatorTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class CalculatorTests
{
    private Calculator _calculator;

    [TestInitialize]
    public void Setup()
    {
        _calculator = new Calculator();
    }

    [TestMethod]
    public void Add_TwoPositiveNumbers_ReturnsSum()
    {
        // Arrange
        int a = 5;
        int b = 10;

        // Act
        int result = _calculator.Add(a, b);

        // Assert
        Assert.AreEqual(15, result);
    }

    [TestMethod]
    public void Add_NegativeAndPositiveNumber_ReturnsCorrectSum()
    {
        // Arrange
        int a = -5;
        int b = 10;

        // Act
        int result = _calculator.Add(a, b);

        // Assert
        Assert.AreEqual(5, result);
    }
}

Explanation of the Test Code

[TestClass]: Indicates that this class contains unit tests.

[TestInitialize]: A method that runs before each test method to set up any necessary objects.

[TestMethod]: Marks a method as a test method.

Assert: Used to verify that the expected outcome matches the actual outcome.

Running Your Tests: To run your tests in Visual Studio:

  • Open the Test Explorer (Test > Windows > Test Explorer).
  • Click on Run All to execute all tests.
  • Check the results in the Test Explorer window.

Best Practices

Keep Tests Independent: Each test should be able to run independently of others.

Use Descriptive Names: Name your test methods clearly to indicate what they are testing.

Test Edge Cases: Consider testing boundary conditions and edge cases.

Refactor Tests: Just like production code, keep your tests clean and maintainable.

Conclusion

Unit testing is an essential practice in C# development that helps ensure code quality and reliability. By following the steps outlined in this presentation, you can start writing effective unit tests for your applications. Remember to keep learning and improving your testing skills!