Debugging failing tests
Make failures reproducible, remove time/randomness, shrink to a minimal case, and improve assertion messages for fast diagnosis.
Game plan
Plan to fix failing tests:
- Reproduce reliably
- Remove flakiness (time/random I/O)
- Shrink to a smallest failing case
- Add clear assertion messages
Flaky by design
Using UtcNow and default Random makes behavior vary between runs → flaky tests.
using System;
// Demo: occasionally fails because it depends on current second and default Random()
public static class Promo
{
// Gives bonus on even seconds; random adds small jitter
public static int BonusNow()
{
int baseBonus = (DateTime.UtcNow.Second % 2 == 0) ? 10 : 9;
Random r = new Random(); // time-based seed → unpredictable
return baseBonus + (r.Next(0, 2)); // 0 or 1 more
}
}
public class Program
{
public static void Main(string[] args)
{
// Simulated test: sometimes expects exactly 10, but result varies
int got = Promo.BonusNow();
if (got != 10) Console.WriteLine("FAIL (flaky): expected 10 but got " + got);
else Console.WriteLine("PASS (maybe): got " + got);
}
}
All lessons in this course
- xUnit/MSTest setup, assertions
- Fakes via interfaces; test data builders
- Debugging failing tests