Skip to content
Tutorials Center
My learning

Topics

Lesson 153 of 182 ยท Testing and Quality

Testing Fundamentals

Learn the basics of testing C# code - unit, integration and end-to-end tests, Arrange-Act-Assert, test doubles, test-driven development and a hand-written mini test runner you can run.

Every program is tested. The only question is whether you test it or your users do. Manual testing means starting the program, typing values, looking at the result and doing it all again after every change. That works for a ten-line script and collapses for anything real. An automated test is a small program that runs your code, checks the result and says "pass" or "fail" in milliseconds. Once you have a few hundred of them, you can change code on Friday afternoon and know before you go home whether you broke something.

This lesson teaches the ideas that every C# test framework builds on: what a test is, the kinds of tests, the Arrange-Act-Assert shape, test doubles, and test-driven development. We will use no framework at all. Our runner has no test framework and no dotnet test command, so we build a tiny runner ourselves. That is also the best way to understand what xUnit, NUnit and MSTest (the next lesson) really do.

What a test is

A test has three parts: some code under test, a situation you set up, and an expectation about the outcome. If the expectation is wrong, the test fails loudly.

You do not need a framework to write one. A comparison and a thrown exception are enough:

C# The smallest possible tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The last check fails because half of 20 is 10, not 11. A failing test is not a disaster; it is the test doing its job. The printed FAIL tells you exactly which expectation did not hold.

This approach already has two weaknesses. If you forget to read the output, a failure goes unnoticed, and when one check throws an exception the remaining checks never run. A real test runner fixes both: each test runs in isolation, failures are collected, and the process reports a non-zero exit code so a build server can stop the pipeline.

The kinds of tests

People argue about names, but three layers are universal:

Kind What it exercises Speed Typical count
Unit test One class or method in isolation, no database, no network, no clock Microseconds to milliseconds Thousands
Integration test Several pieces together: your code plus a real database, file system or an in-process web server Tens of milliseconds to seconds Hundreds
End-to-end (E2E) test The whole system from the outside, such as a browser clicking through a deployed site Seconds to minutes Dozens

They are often drawn as a pyramid: many fast unit tests at the bottom, fewer integration tests in the middle and a handful of E2E tests at the top. The shape is a guideline about cost. The higher you go, the slower tests run, the more they depend on things outside your control, and the harder it is to find why a test failed.

A good unit test has these properties, sometimes remembered as FIRST:

  • Fast: you run them constantly, so they must take almost no time.
  • Isolated: one test never depends on another having run first.
  • Repeatable: same result every time, on every machine. No dependence on the current time, random numbers or network.
  • Self-validating: pass or fail, with no human reading of output.
  • Timely: written close to the code they check.

"Unit" is deliberately vague. Some teams mean one method, some mean one class, some mean one behavior that may span three small classes. What matters is that the test is fast and isolated and checks behavior a caller cares about.

Arrange, Act, Assert

Almost every good test has the same three steps, in this order:

  1. Arrange: create the objects and data the scenario needs.
  2. Act: do the one thing you are testing.
  3. Assert: check the outcome.

Keeping them visibly separate (a blank line between them is enough) makes a test readable at a glance. If you find yourself writing Act, Assert, Act, Assert, you are probably testing two behaviors and should split the test.

C# Arrange, Act, Assert in a shopping cart

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Notice the test checks what the cart does (its total and count), not how it stores lines. If you later replace the list with a dictionary, this test should still pass. Tests that peek into private details break every time you refactor, and people soon stop trusting them.

Naming tests

A test name is documentation that shows up in the failure report. Include the situation and the expected result: Total_WithTwoLines_SumsPriceTimesQuantity or Add_WithZeroQuantity_Throws. When the build fails at night, the name alone should tell the next developer what broke.

A mini test runner

Frameworks automate the boring part: finding test methods, running each one in isolation and reporting. The finding is done with reflection and a custom attribute that marks test methods. Here is a complete miniature version, about forty lines:

C# Build your own test framework

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Study what the runner does, because every framework does the same:

  1. Discovery: find methods marked [Test] with reflection.
  2. Isolation: create a new instance of the test class for each test, so fields set in one test never leak into the next.
  3. Execution and classification: invoke the method. An AssertionFailedException means the code produced a wrong answer (a failure). Any other exception means something unexpected blew up (an error). Real frameworks distinguish these too.
  4. Reporting: print a line per test and a summary, and a real runner sets a non-zero process exit code when anything failed.

BrokenExpectation fails because the expectation is wrong. BugInTheCode errors because the code throws an InvalidOperationException the test did not expect.

Testing failure paths

A test suite that only checks the happy path gives false confidence. Production bugs hide in edge cases: empty input, zero, the maximum value, null, text with unusual characters. For every method, ask yourself:

  • What is the smallest valid input? The largest?
  • What happens just below and just above a boundary? (If the limit is 100, test 99, 100 and 101.)
  • What invalid input should be rejected, and with which exception?
  • What if a collection is empty, or has exactly one element?
C# Boundary values and invalid input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The eight values were chosen on purpose: both sides of each boundary (12/13 and 17/18) and both ends of the valid range (0, 120) plus one step outside (-1, 121). That is boundary-value analysis, and it finds far more bugs than random samples. Off-by-one mistakes (< versus <=) live exactly at these points.

Test doubles: replacing the awkward parts

Real code talks to databases, clocks, email servers and web APIs. A unit test must not, because those things are slow, flaky and have side effects. The solution is to put each awkward dependency behind an interface and give the test a stand-in. All stand-ins are called test doubles, and there are a few kinds:

Double Purpose
Dummy Passed to satisfy a parameter but never used.
Stub Returns canned answers ("the exchange rate is 2").
Fake A working but simplified implementation, such as an in-memory repository.
Spy Records how it was called so the test can look afterwards.
Mock A spy with expectations built in; it fails the test if it was used wrongly.

The mocks, stubs and fakes lesson goes deep. Here is a first taste, with a stub for the clock and a spy for the mailer:

C# A stub and a spy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Because the real DateTime.Now is out of the picture, this test gives the same answer today, tomorrow and in a different time zone. That is repeatability, and it is a direct result of passing dependencies in through the constructor (dependency injection). Code that calls DateTime.Now or new SmtpClient() deep inside a method is hard to test; design for testability and you usually get better design as a bonus.

Test-driven development

Test-driven development (TDD) flips the usual order. You write the test first, watch it fail, then write just enough code to make it pass, then clean up. The cycle is called red, green, refactor:

  1. Red: write a small failing test for the next behavior. (A test that cannot fail proves nothing.)
  2. Green: write the simplest code that makes it pass, even if it is ugly.
  3. Refactor: improve the code while the tests stay green.

TDD works best when you know what you want but not yet how to build it. It keeps steps tiny and gives you a safety net from the first minute. Even if you do not practice it strictly, writing the test for a bug before fixing it is always worth it: you prove you can reproduce the bug and you prevent it from coming back.

Here is a compressed TDD session for a function that turns a title into a URL slug. Each stage adds a test, shows the old code failing it, and moves on. The "implementations" are small static methods so you can compare how the behavior grows:

C# Red, green, refactor in three steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Read the output from top to bottom: V1 passes only the first test, V2 the first two, and V3 all three. In a real TDD session you would only ever see the code for the current step, but the table shows why the next test was needed: each one forced the code to learn something new. Your next step might be a test for an empty title, or for accented characters, which V3 drops completely. Writing that test will tell you what to do about it.

Common pitfalls

  • Testing the framework or the language. A test that checks that List<int>.Add adds an item tests Microsoft's code, not yours.
  • Logic in tests. Loops and if statements inside a test can contain bugs themselves. Prefer plain, literal expected values.
  • Shared mutable state. Static fields and shared databases make tests depend on order. When tests fail only when run together, this is the usual cause.
  • Brittle assertions. Asserting the exact text of a log message or the order of unrelated items makes tests break on harmless changes.
  • Over-mocking. If a test needs six mocks and verifies every call, it mirrors the implementation instead of its behavior. Prefer fakes and real objects where they are cheap.
  • Ignoring flaky tests. A test that sometimes fails teaches the team to ignore red. Fix it or delete it.
  • Chasing 100% coverage. Coverage tells you which lines ran, not whether anything was checked. A test with no assertion gives full coverage and zero safety.

When a bug report arrives, write a failing test that reproduces it first. Then fix the code. The bug can now never return unnoticed, and the test documents the exact case that went wrong.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the correct order of the three steps in a well-structured test?

    You first set up the scenario (Arrange), then perform the one operation under test (Act), then check the outcome (Assert).
  2. 2. Why should a unit test not call DateTime.Now directly through the code under test?

    A test that depends on the real clock can pass today and fail tomorrow. Put the clock behind an interface and give the test a fixed one.
  3. 3. In the test-driven development cycle, what do you do in the 'red' step?

    Red means a failing test. It proves the test can fail, and it defines the next piece of behavior before any code exists.
  4. 4. Which test double records how it was called so the test can inspect it afterwards?

    A spy remembers calls (like the SpyMailer's Sent list). A stub only returns canned answers, and a dummy is never used.
  5. 5. A rule says valid ages are 0 to 120. Which set of inputs best follows boundary-value analysis?

    Bugs cluster at boundaries. Testing both edges and the first value outside each edge catches off-by-one mistakes such as using < instead of <=.

Summary

  • An automated test runs your code, compares the result with an expectation and reports pass or fail without a human reading output.
  • Many fast unit tests, fewer integration tests and a few end-to-end tests form the testing pyramid.
  • Structure each test as Arrange, Act, Assert, check behavior rather than internals, and give tests names that describe situation and result.
  • Test boundaries and invalid input, not just the happy path.
  • Put clocks, databases and mailers behind interfaces so tests can substitute dummies, stubs, fakes, spies and mocks.
  • A test framework discovers [Test] methods by reflection, runs each in a fresh instance, classifies failures and reports; you can write a working one in a few dozen lines.
  • TDD is red, green, refactor: failing test first, simplest passing code, then clean up.

Next: xUnit, NUnit and MSTest