Understanding Unit Testing in C#

Osama HaiDer
2 min readJan 18, 2025

--

Unit testing is a crucial practice for ensuring the quality of your code. It helps catch bugs early, makes your codebase more reliable, and allows for confident refactoring. In this blog, we’ll discuss the benefits of unit testing and explain the AAA scheme with simple examples in C#.

What is Unit Testing?

Unit testing is a way to test individual pieces (or “units”) of code, like a function or method, to ensure they work as expected. These tests are small, focused, and should run quickly.

Benefits of Unit Testing

  1. Catch Bugs Early
    Unit tests verify that your code works as intended before production. This reduces costly bugs later in the development cycle.
  2. Confidence to Refactor
    With unit tests in place, you can safely update or refactor code. If something breaks, the tests will catch it.
  3. Improved Code Quality
    Writing tests encourages clean and modular code because tightly coupled or poorly designed code is harder to test.
  4. Documentation
    Unit tests can document your code by showing how functions are expected to behave.

What is the AAA Scheme?

The AAA scheme stands for:

  1. Arrange: Set up the necessary objects, data, and conditions.
  2. Act: Perform the action you want to test.
  3. Assert: Verify that the expected outcome matches the actual outcome.

This structure keeps your tests clean and easy to understand.

Example: Testing a Calculator Class

Let’s create a simple calculator class and write a unit test for its Add method using the AAA scheme.

Calculator Class

public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}

Unit Test with AAA Scheme

using Xunit;

public class CalculatorTests
{
[Fact]
public void Add_ShouldReturnCorrectSum()
{
// Arrange
var calculator = new Calculator();
int number1 = 5;
int number2 = 3;

// Act
int result = calculator.Add(number1, number2);

// Assert
Assert.Equal(8, result);
}
}

Breaking It Down

  1. Arrange
  • Created an instance of Calculator.
  • Defined the input numbers (5 and 3).

2. Act

  • Called the Add method and stored the result.

3. Assert

  • Verified that the result equals 8.

Best Practices for Unit Testing

  1. Write Small and Independent Tests
    Each test should focus on one functionality. Avoid dependencies on external systems.
  2. Use Meaningful Test Names
    Test names should clearly describe the scenario being tested. Example: Add_ShouldReturnCorrectSum.
  3. Run Tests Frequently
    Run your tests after every change to ensure nothing breaks.
  4. Mock Dependencies
    Use mock objects to replace external dependencies like databases or APIs. This keeps your tests fast and focused.

Conclusion

Unit testing is essential for building reliable software. By following the AAA scheme, you can write clear and maintainable tests that ensure your code works as expected. Start small, and over time, your confidence in your codebase will grow.

--

--

Osama HaiDer
Osama HaiDer

Written by Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#

No responses yet