C#
Assert
Learn assertions with Assert.Equal, Assert.True and Assert.Throws.
The Assert class
Assert is a static class that compares expected and actual state. Common methods: Assert.Equal(expected, actual) for equality, Assert.True(condition) to require truth, Assert.Throws<T>(action) to require a specific exception.
Each Assert throws on failure; that exception marks the test as failed. The clearer the assertion, the more useful the error message. So Assert.Equal(5, result) is preferred; Assert.True(result == 5) gives less information back.
Exercise Triple assertion for MathHelper
Define FactAttribute and a static Assert class. Include Assert.Equal<T>, Assert.True(bool), and Assert.Throws<T>(Action); each throws Exception on failure. MathHelper.IsEven(int) returns true for even, false for odd, and throws ArgumentException below zero. Write three [Fact] methods in MathHelperTests: TestFourIsEven calls Assert.Equal(true, MathHelper.IsEven(4)); TestThreeIsNotEven calls Assert.True(!MathHelper.IsEven(3)); TestNegativeThrows calls Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1)). Main runs all three and prints "Test passed: IsEven checks".
Show example solution
Define static class Assert { public static void Equal<T>(T e, T a) { if (!EqualityComparer<T>.Default.Equals(e, a)) throw new Exception(); } public static void True(bool c) { if (!c) throw new Exception(); } public static void Throws<T>(Action a) where T : Exception { try { a(); } catch (T) { return; } throw new Exception(); } }. Write class MathHelper { public static bool IsEven(int n) { if (n < 0) throw new ArgumentException(); return n % 2 == 0; } }. Write class MathHelperTests { [Fact] public void TestFourIsEven() { Assert.Equal(true, MathHelper.IsEven(4)); } [Fact] public void TestThreeIsNotEven() { Assert.True(!MathHelper.IsEven(3)); } [Fact] public void TestNegativeThrows() { Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1)); } }. In Main, call all three and Console.WriteLine("Test passed: IsEven checks");
Example solution:
using System;
class FactAttribute : Attribute { }
static class Assert
{
public static void Equal<T>(T expected, T actual)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw new Exception($"Expected {expected}, got {actual}");
}
public static void True(bool condition)
{
if (!condition) throw new Exception("Expected: true");
}
public static void Throws<T>(Action action) where T : Exception
{
try { action(); }
catch (T) { return; }
throw new Exception("Expected exception was not thrown");
}
}
class MathHelper
{
public static bool IsEven(int n)
{
if (n < 0) throw new ArgumentException();
return n % 2 == 0;
}
}
class MathHelperTests
{
[Fact]
public void TestFourIsEven()
{
Assert.Equal(true, MathHelper.IsEven(4));
}
[Fact]
public void TestThreeIsNotEven()
{
Assert.True(!MathHelper.IsEven(3));
}
[Fact]
public void TestNegativeThrows()
{
Assert.Throws<ArgumentException>(() => MathHelper.IsEven(-1));
}
}
class Program
{
static void Main()
{
var tests = new MathHelperTests();
tests.TestFourIsEven();
tests.TestThreeIsNotEven();
tests.TestNegativeThrows();
Console.WriteLine("Test passed: IsEven checks");
}
}
Assert.Throws
Assert.Throws<T>(action) expects an action to throw a specific exception. The error path must also be tested; otherwise only the happy path is covered. With a lambda it is short: Assert.Throws<ArgumentException>(() => Parse("")).
If the action does not throw, or throws a different type, the test fails. This verifies that the contract ("empty input is rejected") is actually enforced.
Test yourself
-
Complete the assertion call
Assert.____(5, Calculator.Add(2, 3));
Show answer
Assert.Equal(5, Calculator.Add(2, 3));
-
Which Assert checks that a condition is true?
- Assert.Equal
- Assert.Same
- Assert.True
- Assert.Throws
Show answer
Assert.True — Correct. Assert.True(condition) expects the condition to be true.
-
Match Assert methods
- Assert.Equal
- Equality of two values
- Assert.True
- Condition is true
- Assert.Throws
- Specific exception is thrown
- Assert.Null
- Value is null
Next lesson: [Theory] and [InlineData]