Lesson 24 of 24
In progress
Capstone: test it and ship it
Add unit tests to the task service, publish a self-contained build, and review the whole application against a checklist.
The last lesson in the track. The task application from the previous two lessons gets tests, a publish step and a review, which is what separates a demo from something you can hand over.
What you will learn
- Create a test project and test the service without files.
- Publish a build that runs without the SDK.
- Review the application against a practical checklist.
The test project
dotnet new xunit -o Tasks.Tests
cd Tasks.Tests
dotnet add reference ../Tasks/Tasks.csproj
dotnet new xunit scaffolds a test project with xUnit. The reference lets the tests see the application’s types.
using Tasks.Domain;
using Tasks.Service;
using Tasks.Storage;
using Xunit;
public class TaskServiceTests
{
private sealed class MemoryStore : ITaskStore
{
private List<Task> tasks = new();
public IReadOnlyList<Task> Load() => tasks;
public void Save(IReadOnlyList<Task> value) => tasks = value.ToList();
}
[Fact]
public void Add_trims_the_title_and_assigns_ids()
{
var service = new TaskService(new MemoryStore());
var first = service.Add(" write docs ", Priority.Normal);
var second = service.Add("review", Priority.Low);
Assert.Equal("write docs", first.Title);
Assert.Equal(1, first.Id);
Assert.Equal(2, second.Id);
}
[Fact]
public void Add_rejects_a_blank_title()
{
var service = new TaskService(new MemoryStore());
Assert.Throws<ArgumentException>(() => service.Add(" ", Priority.Low));
}
[Fact]
public void Complete_returns_false_for_an_unknown_id()
{
var service = new TaskService(new MemoryStore());
Assert.False(service.Complete(99));
}
}
The in-memory store is six lines and removes the file system from the tests. That is the payoff of the interface: the service’s rules are tested in milliseconds, deterministically.
[Fact] marks a test with no parameters. Assert.Equal(expected, actual) and Assert.Throws<T> are the two assertions you will use most.
Parameterised tests
[Theory]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("ok", true)]
public void Add_accepts_only_non_blank_titles(string title, bool accepted)
{
var service = new TaskService(new MemoryStore());
if (accepted)
Assert.Equal("ok", service.Add(title, Priority.Low).Title);
else
Assert.Throws<ArgumentException>(() => service.Add(title, Priority.Low));
}
[Theory] with [InlineData] runs the same test with several inputs, and a failure names the failing row. That is the cheapest way to cover boundaries.
Running the tests
dotnet test
The command builds the solution, runs every test and reports pass and fail counts. In CI, a non-zero exit code fails the build, which is what makes the tests a gate rather than a suggestion.
Publishing
dotnet publish -c Release -r linux-x64 --self-contained false -o out
dotnet publish -c Release -r linux-x64 --self-contained true -o out-standalone
The first produces a folder that needs the .NET runtime installed. The second includes the runtime, so the target machine needs nothing — at the cost of a much larger folder.
For a console tool, a single file is convenient:
dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -o out
Trimming can shrink the output further and can break reflection-based code; test the published build before shipping it.
Running the published app
cd out
./Tasks
The working directory matters again: a relative data/tasks.json resolves against wherever the process was started. Use AppContext.BaseDirectory for files that ship with the app, and let the user’s data live in a path from configuration.
A review checklist
- Add a task, quit, restart: does it persist?
- Add a blank title: is the message clear and the app still running?
- Complete an unknown id: does it say so without crashing?
- Delete the data file: does it start with an empty list?
- Run
dotnet test: do all tests pass? - Publish and run the output: does it work outside the project folder?
Those six checks catch most of what goes wrong in a small application, and they take five minutes.
What the structure bought you
- The domain has no I/O, so its rules are tested directly.
- The store is behind an interface, so tests need no files and the storage can change.
- The menu and the API both call the service, so the rules exist once.
Program.csis wiring, so the dependencies are visible in one place.
None of that required a framework. It required deciding which layer owns which decision, which is the part that transfers to a larger codebase.
Where to go next
- Entity Framework Core for a real database, keeping the repository interface.
- Authentication with ASP.NET Core Identity or an external provider.
- Integration tests with
WebApplicationFactory. - Deployment to a container or an app service, with configuration from the environment.
The application you built is small enough to hold in your head and structured enough to grow. That is the useful place to stop.
Testing through the file store and calling it a unit test. A test that writes to disk is slower, can fail because of permissions, and leaves files behind. Use an in-memory implementation for the rules.
Add a test that completing a task makes it disappear from List(onlyOpen: true) while remaining in List(onlyOpen: false), using the in-memory store.
Show solution
[Fact] public void Completing_a_task_hides_it_from_the_open_list() { var service = new TaskService(new MemoryStore()); var task = service.Add("ship", Priority.Urgent); Assert.True(service.Complete(task.Id)); Assert.Empty(service.List(onlyOpen: true)); Assert.Single(service.List(onlyOpen: false)); } The test names the rule and checks both lists, because the interesting behaviour is the difference between them. Assert.Empty and Assert.Single state the expectation more clearly than comparing counts.
Recap
- Test the service with an in-memory store; use
[Theory]for tables of inputs. dotnet publishproduces a deployable folder, self-contained or framework-dependent.- Review the app end to end, including a published build, before calling it done.
That completes C# and .NET: from the SDK to a tested application with a console and an HTTP interface.
xUnit, NUnit or MSTest?
Should tests run in CI?
Progress is saved in this browser only. No account, no email.