A unit test runs one small piece of logic (a method or class) in isolation and checks that it gives the expected result. Tests run in milliseconds, automatically.
Benefits:
Catch bugs early, when they are cheap to fix.
Safe refactoring: if the tests still pass after you reorganise code, you probably did not break anything.
Living documentation: tests show how the code is supposed to behave.
Faster development over time, because you spend less time on manual re-testing and debugging.
A simple xUnit example:
[Fact]
public void Discount_For_Vip_Is_Ten_Percent()
{
var price = new PriceCalculator().Apply(1000m, isVip: true);
Assert.Equal(900m, price);
}
Tips: test behaviour, not implementation details; cover edge cases (zero, empty, null, limits); keep tests independent and readable (arrange, act, assert); mock only external things such as databases or APIs.
You do not need 100% coverage. Focus on important logic and bugs that have happened before.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
A unit test runs one small piece of logic (a method or class) in isolation and checks that it gives the expected result. Tests run in milliseconds, automatically.
Benefits:
A simple xUnit example:
Tips: test behaviour, not implementation details; cover edge cases (zero, empty, null, limits); keep tests independent and readable (arrange, act, assert); mock only external things such as databases or APIs.
You do not need 100% coverage. Focus on important logic and bugs that have happened before.