A test project without a framework
A test of a library needs two projects: the library, which the application also uses, and a program that references it and asserts things about it. Without a framework the second project is a console application, and its status is the status of the suite. This page builds both with the CLI and runs the suite the way a script would.
The layout
Two folders, two project files and two source files. The names are the ones the CLI gives the templates; the test project is called Calc.Tests so that a search for Tests finds it and a reader of the repository sees which project tests which:
src/
Calc/
Calc.csproj
Calculator.cs
tests/
Calc.Tests/
Calc.Tests.csproj
Program.cs
The three commands of the first step are the templates of lesson 003 and the reference of the library lesson (lesson 101):
dotnet new classlib -o src/Calc
dotnet new console -o tests/Calc.Tests
dotnet add tests/Calc.Tests reference src/Calc
The library
The library is a class library project with one static class. The division throws for a zero divisor and the ternary says so on one line, which gives the tests a branch to cover:
namespace Calc;
public static class Calculator
{
public static int Add(int left, int right) => left + right;
public static int Divide(int left, int right) => right == 0
? throw new DivideByZeroException("the divisor is zero")
: left / right;
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
A class library has no entry point, so the block above is not a program that runs on its own: dotnet build of the project is its check, and a reader who copies the file alone gets error CS5001, the error of a program without a Main.
The test project
The test project is a console application with a reference to the library. Its Program.cs is a whole program with the helper of the previous page in it, and the ProjectReference is what lets it write using Calc;:
using Calc;
Console.WriteLine("calculator tests 1.0");
var check = new Check();
check.Equal(5, Calculator.Add(2, 3), "add");
check.Equal(5, Calculator.Divide(10, 2), "divide");
check.Throws<DivideByZeroException>(() => Calculator.Divide(1, 0), "divide by zero");
Console.WriteLine($"checks {check.Passed} passed {check.Failed} failed");
return check.Failed == 0 ? 0 : 1;
internal sealed class Check
{
public int Passed { get; private set; }
public int Failed { get; private set; }
public void Equal<T>(T expected, T actual, string name)
{
if (EqualityComparer<T>.Default.Equals(expected, actual))
{
Passed++;
Console.WriteLine($"ok {name}");
return;
}
Failed++;
Console.WriteLine($"FAIL {name}: expected {expected}, got {actual}");
}
public void Throws<TException>(Action action, string name) where TException : Exception
{
try
{
action();
}
catch (TException)
{
Passed++;
Console.WriteLine($"ok {name}");
return;
}
Failed++;
Console.WriteLine($"FAIL {name}: nothing of type {typeof(TException).Name} was thrown");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\Calc\Calc.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
The reference is one line in the project file and one command: dotnet add tests/Calc.Tests reference src/Calc, which printed the relative path it wrote:
dotnet add tests/Calc.Tests reference src/Calc
Reference `..\..\src\Calc\Calc.csproj` added to the project.
A path written by hand works as well, and the command is the one that gets the separators right on every system.
The run
dotnet run --project builds the test project and the library it references, runs the program and keeps its exit status:
dotnet run --project tests/Calc.Tests
echo "status: $?"
calculator tests 1.0
ok add
ok divide
ok divide by zero
checks 3 passed 0 failed
The status of the run is the status of the program, which is what a script or a build server reads. A test project of this kind is invisible to dotnet test: the next pages show that a console project is not a test project, and what the missing test SDK does to the status.
The next page adds the part a table of hand-written cases cannot give: generated inputs, a seed, and a property that either holds for every case or produces a counterexample.
Exercises
- Add a method to the library that the tests do not call and watch the coverage page count it as an uncovered line.
- Move the helper of the test program into its own file and add it to the project.
- Run the suite from a shell script in the repository root and print its status.
- Add a second library project and have the tests reference both.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a ProjectReference do?
Answer
It builds the referenced project and makes its public types available
The test program writes using Calc; because of the reference.
-
What is the exit status of dotnet run --project?
Answer
The exit status of the program it ran
The measured run printed three passed and status 0.
-
What does a class library without an entry point produce when it is built?
Answer
A dll, with no error, because a library needs no Main
CS5001 appears only when the same file is compiled as an Exe.
-
What does this program print?
var path = Path.Combine("src", "Calc", "Calc.csproj"); Console.WriteLine(path);Answer
src/Calc/Calc.csproj
Path.Combine uses the separator of the system it runs on.
-
Why is the test project a console application and not a library?
Answer
It needs a Main to run without a test framework
The driver runs as a program and returns its status.
-
What does this program print?
var zero = 0; try { Console.WriteLine(1 / zero); } catch (DivideByZeroException ex) { Console.WriteLine(ex.GetType().Name); }Answer
DivideByZeroException
Integer division by zero throws the exception the test asserts.
-
What does dotnet add reference change?
Answer
The project file, with one ProjectReference item
The page shows the item and the line the command printed.
-
What does this program print?
Console.WriteLine(string.Join(", ", new[] { "Calc", "Tests" }));Answer
Calc, Tests
Join puts the separator between the two names.
-
Why is the library a separate project at all?
Answer
The application and the tests use the same compiled code
The tests assert the same code the application runs.
-
What does this program print?
Console.WriteLine(10 / 2); Console.WriteLine(1.0 / 0.0);Answer
5 and Infinity
The integer division is five and the double division is an infinity.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev