xUnit/MSTest setup, assertions
Learn test shape (Arrange-Act-Assert), see how assertions fail tests, and map the ideas to MSTest/xUnit attributes.
xUnit/MSTest setup, assertions is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}
Assertion behavior
An assertion throws on mismatch. Real frameworks work similarly (richer API, better reports).
using System;
public static class Assert
{
public static void AreEqual(int expected, int actual)
{
if (expected != actual)
throw new Exception("AreEqual failed: expected " + expected + ", actual " + actual);
}
public static void IsTrue(bool condition, string message)
{
if (!condition) throw new Exception("IsTrue failed: " + message);
}
}
public static class MathUtil
{
public static int Mul(int a, int b) { return a * b; }
}
public class Program
{
public static void Main(string[] args)
{
try
{
int r = MathUtil.Mul(2, 4);
Assert.AreEqual(8, r); // pass
Assert.IsTrue(r % 2 == 0, "even"); // pass
Console.WriteLine("All assertions passed.");
}
catch (Exception ex)
{
Console.WriteLine("TEST FAILED: " + ex.Message);
}
}
}
Passing tests example
Each test checks one behavior. In real MSTest/xUnit a failing Assert marks the test red.
using System;
public static class StringUtil
{
public static string Repeat(string s, int n)
{
if (n < 0) throw new ArgumentOutOfRangeException("n");
string r = "";
for (int i = 0; i < n; i++) r += s;
return r;
}
}
public static class Assert
{
public static void AreEqual(string expected, string actual)
{
if (expected != actual)
throw new Exception("AreEqual failed: expected [" + expected + "], actual [" + actual + "]");
}
}
public class Program
{
static void TestRepeat_Happy()
{
string got = StringUtil.Repeat("ab", 3);
Assert.AreEqual("ababab", got); // pass
}
static void TestRepeat_EmptyTimesZero()
{
string got = StringUtil.Repeat("x", 0);
Assert.AreEqual("", got); // pass
}
public static void Main(string[] args)
{
try { TestRepeat_Happy(); Console.WriteLine("PASS: Happy"); } catch (Exception ex) { Console.WriteLine("FAIL: Happy -> " + ex.Message); }
try { TestRepeat_EmptyTimesZero(); Console.WriteLine("PASS: Zero"); } catch (Exception ex) { Console.WriteLine("FAIL: Zero -> " + ex.Message); }
}
}
MSTest vs xUnit mapping
MSTest basics:
- [TestClass] on the class
- [TestMethod] on each test
- Assert.AreEqual/IsTrue/AreNotEqual…
xUnit basics:
- [Fact] for a test method
- Assert.Equal/True/False…
- No test class attribute needed
Naming & scope tips
Tips:
- Name tests like Method_Scenario_ExpectedResult
- One assert or one behavior per test
- Keep tests tiny and independent
Assertions cause failures
Recap
Recap: Use the AAA pattern, compare expected vs actual with assertions, and map to MSTest ([TestMethod]) or xUnit ([Fact]).
Frequently asked questions
Is the “xUnit/MSTest setup, assertions” lesson free?
Yes — the full text of “xUnit/MSTest setup, assertions” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “xUnit/MSTest setup, assertions”?
Learn test shape (Arrange-Act-Assert), see how assertions fail tests, and map the ideas to MSTest/xUnit attributes. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “xUnit/MSTest setup, assertions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- xUnit/MSTest setup, assertions
- Fakes via interfaces; test data builders
- Debugging failing tests