Testing the C# core with a hand-written driver
The core is a class library, so its tests are a console project that references it, in the shape the testing group of this site builds by hand. The driver is a table: one request line and the answer the core must give, one line of output per case, and a status that a script reads.
The test project
A console project with a ProjectReference to the core, and no package:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Core\Core.csproj" />
</ItemGroup>
</Project>
The driver
Eight cases: three good requests with their three amounts, and five bad ones that cover each rule of the protocol, including the rounding case of three units at 0.333:
using Core;
Console.WriteLine("core tests 1.0");
var cases = new (string Line, string Expected)[]
{
("sku=pen qty=2 unit=1.50", "ok 3.00 0.60 3.60"),
("sku=paper qty=10 unit=0.25", "ok 2.50 0.50 3.00"),
("sku=pen qty=3 unit=0.333", "ok 1.00 0.20 1.20"),
("sku=pen qty=0 unit=1.50", "err qty must be 1 to 100"),
("sku=pen qty=2 unit=0", "err unit must be 0.01 to 10000"),
("sku=pen qty=2", "err format expected sku= qty= unit="),
("sku=pen qty=2 unit=1.50 extra", "err format expected sku= qty= unit="),
("sku=pe-n qty=2 unit=1.50", "err sku must be 1 to 20 letters or digits"),
};
var failed = 0;
foreach (var (line, expected) in cases)
{
var actual = Pricing.Process(line);
var ok = actual == expected;
if (!ok)
{
failed++;
}
Console.WriteLine($"{(ok ? "ok " : "FAIL")} {actual}");
}
Console.WriteLine($"cases {cases.Length} failed {failed}");
return failed == 0 ? 0 : 1;
The run
dotnet run -c Release
echo "status: $?"
core tests 1.0
ok ok 3.00 0.60 3.60
ok ok 2.50 0.50 3.00
ok ok 1.00 0.20 1.20
ok err qty must be 1 to 100
ok err unit must be 0.01 to 10000
ok err format expected sku= qty= unit=
ok err format expected sku= qty= unit=
ok err sku must be 1 to 20 letters or digits
cases 8 failed 0
status: 0
Each line of the output is the answer of the core, not the expected one, so a reader sees what the core said and the ok or FAIL prefix says whether it was right. The rounding case is the interesting one: three at 0.333 is 0.999, which rounds away from zero to 1.00, and the tax of 1.00 is 0.20, so the total is 1.20 rather than 1.19.
The last line is the summary a build server reads, and the return value of the program is the same number: zero when every case passed. The testing group of this site builds the same driver with an assertion helper and a shell script around it; this page keeps it to the table, which is enough for a core of two hundred lines.
The last page is deployment: the image that would hold both halves.
Exercises
- Add a case for a unit price of 10000.01 and check the message.
- Make the driver print the expected answer next to the actual one on a failure.
- Move the table into a text file and read it in the driver.
- Add a case that exercises the invariant culture with a comma decimal.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is the test project a console application?
Answer
It needs a Main to run without a test framework
The driver is the runner.
-
How many cases does the table have?
Answer
Eight
The measured summary says cases 8 failed 0.
-
What does this program print?
Console.WriteLine("ok 1.00 0.20 1.20");Answer
ok 1.00 0.20 1.20
Three at 0.333 rounds to 1.00 and the tax makes 1.20.
-
What is the status of the run?
Answer
0, because every case passed
The measured run ends with status 0.
-
What does this program print?
Console.WriteLine("cases 8 failed 0");Answer
cases 8 failed 0
That is the summary line of the measured run.
-
What is on each line of the output?
Answer
The answer of the core and an ok or FAIL prefix
The page explains the choice of printing the actual answer.
-
What does this program print?
Console.WriteLine("ok 1.00".StartsWith("ok"));Answer
True
The rounding case passes, so its prefix is ok.
-
What does the driver return when a case fails?
Answer
1
The return value is the status of the suite.
-
Which case covers the rounding rule?
Answer
Three units at 0.333
It is the case of the page that exercises the cent rounding.
-
What does this program print?
var subtotal = Math.Round(3 * 0.333m, 2, MidpointRounding.AwayFromZero); var tax = Math.Round(subtotal * 0.20m, 2, MidpointRounding.AwayFromZero); Console.WriteLine($"{subtotal + tax:F2}");Answer
1.20
The total of the rounding case is 1.20 with the tax.
The C# code on this page was built with the .NET SDK 10.0.303 and the Python code was run with Python 3.14.10, on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev