Testing C# code

A test is a program that runs another program and decides whether it did what it promised. The decision has three parts: an assertion that compares a value with an expectation, a driver that counts the assertions that failed, and an exit status that a shell reads. None of the three needs a package, and all of them are in this group as complete programs that were built and run in the container described at the end of each page.

The group starts without a framework, because a test framework is a large version of the same three parts: xUnit, NUnit and MSTest each bring an assertion library, a runner and a way to discover tests. The pages that use them show the real runs of this container: the xUnit template and coverlet are in the image, and the other packages were restored from nuget.org while the page was written, which the last page records with the version of every package. A restore that finds no feed stops with NU1301, and that error is on the same page, measured.

The capstones of the C# course already end with tests: the log report has a table of good and bad lines with a driver (lesson 119) and the calculator has a table of expressions with a tolerance (lesson 126). These pages take the same idea and go into the parts a real suite needs: a project of its own, generated cases, files that hold the expected output, and the tools that a team asks for next.

A test driver and its assertions An assertion helper of three methods, a driver that counts failures, and the status of a green and a red run. Open A test project without a framework A class library, a console project that references it, and the run that gives the suite its status. Open Property checks with a seeded generator One hundred cases per property from a hand-written generator with a seed, and a counterexample found by a wrong property. Open Golden files A report compared with the file that holds the expected text, and the diff of a change with its status. Open The exit status is the result A driver, a shell script that keeps its status, and the four measured statuses of the pair. Open dotnet test with xUnit A fact, a theory, a failing test and its real message, a filter, and the restore error of an unreachable feed. Open Coverage with coverlet The coverage collector, the cobertura file it writes, a program that summarises it, and three measured coverage states. Open NUnit, MSTest, Moq, FluentAssertions and BenchmarkDotNet The other frameworks and libraries, each installed and run here, the project-name collision, and the NU1301 of an unreachable feed. Open

Start with the driver and its assertions if you have never written a test, and with dotnet test with xUnit if your team already uses a framework. The page on coverage is the one to read before trusting a percentage, and the package page is the reference for what each framework and library is for.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What are the three parts of a test without a framework?

    Answer

    An assertion, a driver that counts failures, and an exit status

    The first page of the group builds all three by hand.

  2. Why does the group start without a test framework?

    Answer

    A framework is a larger version of the same three parts, and the smaller one is easier to see

    The xUnit page then shows what the framework adds around the same idea.

  3. Which page builds a library and a console project that references it?

    Answer

    The page on the project layout

    It shows the ProjectReference and the run that gives the suite its status.

  4. What does a seed make repeatable?

    Answer

    The sequence of generated inputs, and with it the failure

    The property page fixes its seeds so its output is the same on every run.

  5. What is a golden file?

    Answer

    A file with the expected output of the program

    The comparison is a diff, and its status is the result.

  6. Which command runs the xUnit suite of a solution?

    Answer

    dotnet test

    The xUnit page runs it at the root of a solution and through a filter.

  7. What does the coverage collector write?

    Answer

    A cobertura XML file under TestResults

    The coverage page reads that file with a program of its own.

  8. Which packages are in the NuGet cache of this image?

    Answer

    The xUnit template packages and coverlet

    The rest were restored from nuget.org for the package page.

  9. What error does a restore report when the feed cannot be reached?

    Answer

    NU1301

    The xUnit page shows the real lines of that error.

  10. Why is a console application with tests in it not a test project?

    Answer

    It has no test SDK, so dotnet test finds no tests and returns zero

    The coverage page measures that green status with no tests.

Every program on these pages was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container with four processors.