xUnit/MSTest setup, assertions
Learn test shape (Arrange-Act-Assert), see how assertions fail tests, and map the ideas to MSTest/xUnit attributes.
What is a unit test?
Goal: Write small, repeatable unit tests.
- Separate test project uses MSTest or xUnit
- Each test checks one behavior
- Use clear names and assertions
AAA pattern
Tests follow Arrange → Act → Assert. Here we simulate an assertion with a simple check.
using System;
public static class MathUtil
{
// Method under test
public static int Add(int a, int b)
{
return a + b;
}
}
public class Program
{
public static void Main(string[] args)
{
// Arrange
int a = 2;
int b = 3;
int expected = 5;
// Act
int actual = MathUtil.Add(a, b);
// Assert (manual check for this console demo)
if (actual == expected)
Console.WriteLine("PASS: Add returned " + actual);
else
Console.WriteLine("FAIL: expected " + expected + " but got " + actual);
}
}
All lessons in this course
- xUnit/MSTest setup, assertions
- Fakes via interfaces; test data builders
- Debugging failing tests