| title | Create a test validation GitHub workflow |
|---|---|
| description | In this quickstart, you will learn how to create a GitHub workflow to test your .NET source code. |
| ms.date | 02/16/2022 |
| ms.topic | 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 testdefines 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
onnode 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
pushorpull_requestoccurs on themainbranch where any files changed ending with the .cs or .csproj file extensions.
- Triggered when a
-
The
envnode 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_VERSIONis assigned the value'6.0.401'. The environment variable is later referenced to specify thedotnet-versionof theactions/setup-dotnet@v3GitHub Action.
- The environment variable
-
The
jobsnode 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. Thenameandruns-onelements are dynamic for each value in thematrix/os. This will run on the latest versions of Ubuntu, Windows, and macOS. - The
actions/setup-dotnet@v3GitHub Action is used to setup the .NET SDK with the specified version from theDOTNET_VERSIONenvironment variable. - The
dotnet restorecommand is called. - The
dotnet buildcommand is called. - The
dotnet testcommand 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