Start learning free

LESSON 155 OF 173 · BACKEND: SHIPPING IT

Run the Tests on Every Push

Practice

Continuous integration (CI) means your tests run by themselves every time you push. On GitHub you ask for it with a workflow: a YAML file in the folder .github/workflows/, made of key: value lines nested by their indentation. The key on says which event starts it, and each job runs its steps from top to bottom on a fresh machine, the runner.

Here is a small workflow of another kind. It starts on every pull request and runs one command:

name: Hello
on: pull_request
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo Hello from the runner

A step either uses a ready-made action, such as actions/checkout, which copies your repository onto the runner, or runs a command, as you would type it in a terminal. The runner starts with its own .NET SDK and nothing of yours: no code, no packages. So the order matters: copy the code, set up the .NET the shop is built with, restore the packages the .csproj files pin with dotnet restore, then run the tests with dotnet test. The job stops at the first step that fails.

You write the workflow as a C# list of strings named workflow, one string per line, each with a comma after it. The check reads it the way GitHub would, runs the job and must print:

Workflow: Tests
Starts on: push
Job test, on a fresh ubuntu-latest machine
Step 1: uses actions/checkout@v6
  copied the repository: shop.sln, Program.cs, shop.csproj, ShopTests/ShopTests.csproj, ShopTests/ShopTests.cs
Step 2: uses actions/setup-dotnet@v5 with dotnet-version 8.0.x
  the .NET 8.0 SDK is ready (.x: its newest patch)
Step 3: run dotnet restore
  restored Microsoft.Data.Sqlite 8.0.11 and xunit 2.9.3, the versions the .csproj files pin
Step 4: run dotnet test
  Passed! Failed: 0, Passed: 3, Total: 3
The job passed: every push now runs the tests on .NET 8.0.

Your task: the starting workflow already has its name and the job test on ubuntu-latest. Add the trigger on: push and four steps: check out the code with actions/checkout@v6, set up .NET '8.0.x' with actions/setup-dotnet@v5, restore the packages with dotnet restore, and run the tests with dotnet test.


Assignment
Follow the Coding Tutorial to write a GitHub Actions workflow that runs the shop's tests on .NET 8.0 on every push.


Hint
Indentation is the structure, and it must be spaces: the step items start 6 spaces in with a dash, with: sits 8 spaces in under its uses, and dotnet-version 10. Write the version in single quotes, '8.0.x', so YAML keeps it as text exactly as written.


STEP 1 OF 7
    Ctrl + Enter to check