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.
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
What error does a restore report when the feed cannot be reached?
Answer
NU1301
The xUnit page shows the real lines of that error.
-
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.