Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 28 / 68

Checking Correctness (Your First Step into Testing)

In this lesson you will learn how to write a simple test in C#, taking your first step towards checking that a program is correct. This is for anyone searching for "C# testing for beginners".

You will learn the basic idea behind test code: using an if statement to confirm that a condition matches what you expected. Checking automatically that a function returns the right result saves you from verifying it by hand every time.

The sample code checks with an if statement whether Add(2, 3) equals 5, printing "Test passed" if it does and "Test failed" if it does not. That is the core idea of testing: confirm the result in code rather than by eye.

A common stumbling block for beginners is checking behaviour manually every time and missing cases. In practice you would use a dedicated framework such as xUnit, but the underlying idea is exactly this assertion.

Bear in mind too that assertions are a debugging aid during development, not a substitute for real error handling in production.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)