title | description | author | ms.author | ms.date | ms.topic |
---|---|---|---|---|---|
Create a test validation GitHub workflow |
In this quickstart, you will learn how to create a GitHub workflow to test your .NET source code. |
IEvangelist |
dapine |
02/16/2022 |
quickstart |
In this quickstart, you will learn how to create a GitHub workflow to test your .NET source code. Automatically testing your .NET code within GitHub is referred to as continuous integration (CI), where pull requests or changes to the source trigger workflows to exercise. Along with building the source code, testing ensures that the compiled source code functions as the author intended. More often than not, unit tests serve as immediate feedback-loop to help ensure the validity of changes to source code.
[!INCLUDE prerequisites]
[!INCLUDE add-github-workflow]
Create a new file named build-and-test.yml, copy and paste the following YML contents into it:
:::code language="yml" source="snippets/dotnet-test-github-action/build-and-test.yml":::
In the preceding workflow composition:
-
The
name: build and test
defines the name, "build and test" will appear in workflow status badges.:::code language="yml" source="snippets/dotnet-test-github-action/build-and-test.yml" range="1":::
-
The
on
node signifies the events that trigger the workflow::::code language="yml" source="snippets/dotnet-test-github-action/build-and-test.yml" range="3-9":::
- Triggered when a
push
orpull_request
occurs on themain
branch where any files changed ending with the .cs or .csproj file extensions.
- Triggered when a
-
The
env
node defines named environment variables (env var).:::code language="yml" source="snippets/dotnet-test-github-action/build-and-test.yml" range="11-12":::
- The environment variable
DOTNET_VERSION
is assigned the value'6.0.401'
. The environment variable is later referenced to specify thedotnet-version
of theactions/setup-dotnet@v3
GitHub Action.
- The environment variable
-
The
jobs
node builds out the steps for the workflow to take.:::code language="yml" source="snippets/dotnet-test-github-action/build-and-test.yml" range="14-37" highlight="2,4-8,13-15,18,21,24":::
- There is a single job, named
build-<os>
where the<os>
is the operating system name from thestrategy/matrix
. Thename
andruns-on
elements are dynamic for each value in thematrix/os
. This will run on the latest versions of Ubuntu, Windows, and macOS. - The
actions/setup-dotnet@v3
GitHub Action is used to setup the .NET SDK with the specified version from theDOTNET_VERSION
environment variable. - The
dotnet restore
command is called. - The
dotnet build
command is called. - The
dotnet test
command is called.
- There is a single job, named
[!INCLUDE add-status-badge]
Passing | Failing | No status |
---|---|---|
:::image type="content" source="media/test-badge-passing.svg" alt-text="GitHub: test passing badge"::: | :::image type="content" source="media/test-badge-failing.svg" alt-text="GitHub: test failing badge"::: | :::image type="content" source="media/test-badge-no-status.svg" alt-text="GitHub: test no-status badge"::: |
- dotnet restore
- dotnet build
- dotnet test
- Unit testing .NET apps
- actions/checkout
- actions/setup-dotnet
[!div class="nextstepaction"] Quickstart: Create a GitHub workflow to publish your .NET app