The Bitwarden Blog
How to set up a testing workflow for a .NET library 2024
AA
geschreven door:Anders Aberg
gepost:
- Blog
- How to set up a testing workflow for a .NET library 2024
Developing a library involves a..
Bash├── examples │ └── ... ├── src │ ├── Passwordless │ │ ├── ... │ │ └── Passwordless.csproj │ └── Passwordless.AspNetCore │ ├── ... │ └── Passwordless.AspNetCore.csproj ├── tests │ ├── Passwordless.Tests │ │ ├── ... │ │ └── Passwordless.Tests.csproj │ └── Passwordless.AspNetCore.Tests │ ├── ... │ └── Passwordless.AspNetCore.Tests.csproj ├── Passwordless.sln └── Directory.Build.props
C#<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462;net6.0;net7.0;netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
</PropertyGroup>
<!-- ... -->
</Project>
Continue...
Bash# Friendly name of the workflow
name: main
# Events that trigger the workflow
# (push and pull_request events with default filters)
on:
push:
pull_request:
# Workflow jobs
jobs:
# ID of the job
test:
# Operating system to run the job on
runs-on: ubuntu-latest
# Steps to run in the job
steps:
# Check out the repository
- uses: actions/checkout@v4 # note that you'd ideally pin versions to hashes, read on to learn more
# Run the dotnet test command
- run: dotnet test --configuration Release
Bash$ dotnet test Microsoft (R) Test Execution Command Line Tool Version 17.8.0 (x64) Copyright (c) Microsoft Corporation. All rights reserved. Starting test execution, please wait... A total of 1 test files matched the specified pattern. Passed! - Failed: 0, Passed: 8, Skipped: 0, Total: 8, Duration: 993 ms - Passwordless.Tests.dll (net6.0) Passed! - Failed: 0, Passed: 8, Skipped: 0, Total: 8, Duration: 993 ms - Passwordless.Tests.dll (net8.0) Passed! - Failed: 0, Passed: 8, Skipped: 0, Total: 8, Duration: 993 ms - Passwordless.Tests.dll (net462) Starting test execution, please wait... A total of 1 test files matched the specified pattern. Passed! - Failed: 0, Passed: 21, Skipped: 1, Total: 22, Duration: 8 s - Passwordless.AspNetCore.Tests.dll (net6.0) Passed! - Failed: 0, Passed: 21, Skipped: 1, Total: 22, Duration: 8 s - Passwordless.AspNetCore.Tests.dll (net8.0)
Bashname: main
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Setup .NET SDK
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- run: dotnet test --configuration Release
Bashname: main
on:
push:
pull_request:
jobs:
test:
# Matrix defines a list of arguments to run the job with,
# which will be expanded into multiple jobs by GitHub Actions.
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
# We can reference the matrix arguments using the `matrix` context object
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- - run: dotnet test --configuration Release
Bash# Testing workflow
name: main
on:
push:
pull_request:
jobs:
test:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- run: >
dotnet test
--configuration Release
--logger "trx;LogFileName=test-results.trx"
# Upload test result files as artifacts, so they can be fetched by the reporting workflow
- uses: actions/upload-artifact@v4
with:
name: test-results
path: "**/*.trx"
# Reporting workflow
name: Test results
on:
# Run this workflow after the testing workflow completes
workflow_run:
workflows:
- main
types:
- completed
jobs:
report:
runs-on: ubuntu-latest
steps:
# Extract the test result files from the artifacts
- uses: dorny/test-reporter@v1
with:
name: Test results
artifact: test-results
path: "**/*.trx"
reporter: dotnet-trx
fail-on-error: true
Bashname: main
on:
push:
pull_request:
jobs:
test:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- run: >
dotnet test
--configuration Release
--logger GitHubActions
Bashname: main
on:
push:
pull_request:
jobs:
test:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- run: >
dotnet test
--configuration Release
--logger GitHubActions
--collect:"XPlat Code Coverage"
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
# Codecov will automatically merge coverage reports from all jobs
- uses: codecov/codecov-action@v3
Bashjobs: test: permissions: contents: read
Bashame: main
on:
push:
pull_request:
jobs:
test:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
6.0.x
- run: >
dotnet test
--configuration Release
--logger GitHubActions
--collect:"XPlat Code Coverage"
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
- uses: codecov/codecov-action@v3
Bashname: main
on:
push:
pull_request:
jobs:
test:
matrix:
os:
- windows-latest
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
permissions:
contents: read
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-dotnet@4d6c8fcf3c8f7a60068d26b594648e99df24cee3 # v4.0.0
with:
dotnet-version: |
8.0.x
6.0.x
- run: >
dotnet test
--configuration Release
--logger GitHubActions
--collect:"XPlat Code Coverage"
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
- uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4