How do i run nunit unit tests from Github Actions - c#

I have a Xamrain Forms app that I wish to run my unit tests on commit how Do I add it into github actions.
Is their a way to block the build if the tests fail
name: CI on Push and Pull Request
on: [push, pull_request]
jobs:
Practice:
runs-on: macos-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
- name: Show what's in the directory
run: |
ls
- name: Show what all is in the parent directory
run: |
cd ..
ls
Android:
runs-on: macos-latest
steps:
- uses: actions/checkout#v1
- name: Android
run: |
nuget restore
cd THEHOCKEYLAB.Android/
msbuild THEHOCKEYLAB.Android.csproj /verbosity:normal /t:Rebuild /p:Configuration=Debug
iOS:
runs-on: macos-latest
steps:
- uses: actions/checkout#v1
- name: iOS
run: |
nuget restore
msbuild THEHOCKEYLAB.iOS.csproj /verbosity:normal /t:Rebuild /p:Platform=iPhoneSimulator /p:Configuration=Debug
The Structure of the project is as such
I am using nunit thanks. Also I need to make a custom nuget for devexpress mobile controls as their provided free they dont have a nuget feed any suggestions here.

Related

Dotnet Semantic Versioning and Release with Github Actions

I have a dotnet project and am trying to build a CI/CD pipeline that does the following using Github Actions:
Builds the project on a PR or master change.
Tests the project on a PR or master change.
On a master change calculates a semver (ideally a changelog too but that's less important).
On a master change uses this semver, builds a nuget package, and publishes it to GitHub and Nuget.
So far, I have this workflow that builds and tests my project. This seems to work nicely. However, it also has a publish step, which never seems to be run (I guess the if is wrong(?)) and I'm really not sure where to start with the SemVer part of what I want.
name: .NET Build, Test and Publish
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
env:
DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
DISCOS_API_URL: http://localhost:3000/api/
steps:
- uses: actions/checkout#v2
with:
submodules: recursive
- name: Build docker stack
run: docker-compose -f src/DISCOSweb-Sdk/DISCOSweb-Sdk.Tests/docker-compose.yml up -d --force-recreate --build
- name: Setup .NET
uses: actions/setup-dotnet#v1
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
- name: Build
run: dotnet build --no-restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
- name: Test
run: dotnet test --no-build --verbosity normal src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
publish: # This stage is never run
if: github.event.pull_request.merged == 'true'
needs: build
runs-on: ubuntu-latest
steps:
- name: Publish
id: publish_nuget
uses: brandedoutcast/publish-nuget#v2
with:
PACKAGE_NAME: DISCOSweb-Sdk
PROJECT_FILE_PATH: "**/DISCOSweb-Sdk.csproj"
BUILD_CONFIGURATION: Release
NUGET_KEY: ${{secrets.NUGET_KEY}}
VERSION_STATIC: 1.0.0 # Replace with something better
I feel like this should be a relatively common and straightforward thing to do so any assistance would be really appreciated.
I managed to accomplish this by splitting my CI and CD workflows, and making use of some very helpful packages.
CI Workflow
This first workflow runs on PRs and pushes to master. It's worth noting that when branch protection is on, a push to master is synonymous with a merged PR.
name: .NET Continuous Integration
on:
pull_request:
branches: [ master ]
push:
branches: [ master ]
jobs:
test:
name: Test Project (Mock API)
runs-on: ubuntu-latest
env:
DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
DISCOS_API_URL: http://localhost:3000/api/
steps:
- uses: actions/checkout#v3
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Build docker stack
run: docker-compose -f ./src/DiscosWebSdk/DiscosWebSdk.Tests/docker-compose.yml up -d --force-recreate --build
- name: Run tests against mock API
run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln
CD Workflow
There's a bit more to the CD workflow. It runs on pushes to master but only when there are changes made to the folder containing the actual source code for the SDK (that's what the paths block does).
I then used the mathieudutour/github-tag-action#v6.0 action to tag the commit with a calculated semantic version (calculated using conventional commits).
The package build is then tagged with this version using the -p:PackageVersion=${{ steps.tag_version.outputs.new_version }} option on dotnet nuget pack.
The ncipollo/release-action#v1 also creates a release on Github.
Finally, building and pushing the release to Github and Nuget is done using dotnet nuget push as usual.
name: .NET Continuous Deployment
on:
push:
branches: [ master ]
paths:
- src/DiscosWebSdk/DiscosWebSdk/**
workflow_dispatch:
jobs:
test:
name: Test Project (Real API)
runs-on: ubuntu-latest
env:
DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
DISCOS_API_URL: https://discosweb.esoc.esa.int/api/
steps:
- uses: actions/checkout#v3
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Test against real API
run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln
semantic-release:
needs: test
name: Create a Package Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3 # Need the full commit history for conventional commit
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action#v6.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Create a GitHub release
uses: ncipollo/release-action#v1
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
- name: Create Nuget Package
run: dotnet build -c Release ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj && dotnet pack -c Release -p:PackageVersion=${{ steps.tag_version.outputs.new_version }} -o . ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj
- name: Upload Package for Publishing
uses: actions/upload-artifact#v3
with:
name: PackedLib
path: ./*.nupkg
github-publish:
needs: semantic-release
name: Publish to Github
runs-on: ubuntu-latest
steps:
- name: Download built project
uses: actions/download-artifact#v3
with:
name: PackedLib
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Push Package to GitHub
run: dotnet nuget push --api-key ${{secrets.GITHUB_TOKEN}} --source "https://nuget.pkg.github.com/hughesjs/index.json" *.nupkg
nuget-publish:
needs: semantic-release
name: Publish to Nuget
runs-on: ubuntu-latest
steps:
- name: Download built project
uses: actions/download-artifact#v3
with:
name: PackedLib
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
- name: Push Package to Nuget
run: dotnet nuget push --api-key ${{secrets.NUGET_KEY}} --source "https://api.nuget.org/v3/index.json" *.nupkg

Couldn't publish 3 projects because of versionize

I'm trying to publish 3 projects as a nuget package. The following GitHub workflow works fine publishing Project1, but it doesn't publish src/Project2/Project2.csproj and src/Project3/Project3.csproj.
I tried to create 3 files with the exact same code but for each one of the projects and there is a conflict in versionize. The first project is being published just fine but the rest of them fail because of the versions.
How can I make it work that way?
name: Publish
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 6.0.x
source-url: https://nuget.pkg.github.com/Xnt/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
- name: Install dotnet tools
run: dotnet tool install --global Versionize
- name: Restore
run: dotnet restore
- name: Build
run: dotnet build -c Release --no-restore
- name: Setup git
run: |
git config --global user.name 'GitHub actions'
git config --global user.email '...'
- name: Version
run: versionize
- name: Push tags
run: |
git push --follow-tags
- name: Pack
run: dotnet pack --no-build -c Release -o . src/Project1/Project1.csproj
- name: Publish
run: dotnet nuget push *.nupkg

Library build with netcoreapp3.1 throws errors on GitHub actions trying to run tests on multiple frameworks

I'm trying to contribute to a great project (https://github.com/billbogaiv/hybrid-model-binding) - I want to update the target framework and set up tests.
My fork: https://github.com/Misiu/hybrid-model-binding/tree/tests
I've updated the project from netstandard2.1 to netcoreapp3.1 and everything works fine.
So now I have 3 projects in the solution:
HybridModelBinding - main library project
AspNetCoreWebApplication - project with samples
HybridModelBinding.UnitTests - project that will contain unit tests
I'd like my tests to run on multiple dotnet versions, so I've added this workflow:
name: .NET Core
# Trigger event on a push or pull request
on: [push, pull_request]
# Jobs that run in parallel
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: [ '3.1.x','5.0.x', '6.0.x' ]
# Steps that run sequentially
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet#v1.7.2
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
So in theory everything should be built and run on 3 runtimes.
The first build works fine (on 3.1.x), but the second and third throw errors:
Test run for
/home/runner/work/hybrid-model-binding/hybrid-model-binding/tests/HybridModelBinding.UnitTests/bin/Debug/netcoreapp3.1/HybridModelBinding.UnitTests.dll
(.NETCoreApp,Version=v3.1) Microsoft (R) Test Execution Command Line
Tool Version 16.11.0 Copyright (c) Microsoft Corporation. All rights
reserved.
Starting test execution, please wait... A total of 1 test files
matched the specified pattern. Testhost process exited with error: It
was not possible to find any compatible framework version The
framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
The following frameworks were found:
5.0.14 at [/home/runner/.dotnet/shared/Microsoft.AspNetCore.App] You can resolve the problem by installing the specified framework
and/or SDK. The specified framework can be found at:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=ubuntu.20.04-x64
. Please check the diagnostic logs for more information.
Test Run Aborted.
3>Done Building Project "/home/runner/work/hybrid-model-binding/hybrid-model-binding/tests/HybridModelBinding.UnitTests/HybridModelBinding.UnitTests.csproj"
(VSTest target(s)) -- FAILED.
1>Done Building Project "/home/runner/work/hybrid-model-binding/hybrid-model-binding/HybridModelBinding.sln"
(VSTest target(s)) -- FAILED.
Build FAILED.
Build summary: https://github.com/Misiu/hybrid-model-binding/runs/5392702331?check_suite_focus=true
I can compile projects locally and run the tests, but I'd like to run them on GitHub, so in the future, we can add more tests and develop the library easier.
Specify the Target framework monikers (TFMs) via include under the matrix and specify the framework for dotnet test by adding the argument -f=${{ matrix.tfm }}.
name: .NET Core
# Trigger event on a push or pull request
on: [push, pull_request]
# Jobs that run in parallel
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- dotnet-version: '3.1.x'
tfm: 'netcoreapp3.1'
- dotnet-version: '5.0.x'
tfm: 'net5.0'
- dotnet-version: '6.0.x'
tfm: 'net6.0'
# Steps that run sequentially
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet#v1.7.2
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Install dependencies
run: dotnet restore -p:TargetFramework=${{ matrix.tfm }}
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal -f=${{ matrix.tfm }}
Sources
https://learn.microsoft.com/en-us/dotnet/standard/frameworks#supported-target-frameworks
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#options
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#using-environment-variables-in-a-matrix

Is there a way to display code coverage results metrix in ci-cd in github?

I'm using code coverage tool in visual studio 2019 enterprise.
I get a .coverage file, but I want to display the matrix result in the CI.
Do you know any way to do that?
Thank you.
P.S
There is a GitHub action danielpalme/ReportGenerator
but it display results to a file. I need the same but display results in the CI-CD run itself.
You'll need to use a code-analysis service that is able to analyse your code-coverage reports generated by dotnet test, ng test or similar. For example codecov.io
Go to codecov.io
Login with your account where your git repos are hosted (Github)
Click your avatar on the top right → Settings → Access
Generate token → name = GithubCI
Copy your token to a temporal notepad
Go to your git repo that's hosted on hosting service (github.com)
Settings tab → secrets
Add your codecov.io token here, either under repository secrets/organization secrets
I then have the following github workflow (for angular) to run the unit tests, which generates the code coverage reports. The workflow then publishes the coverage reports to codecov.io
name: npm-publish
on:
push:
branches: [ master ]
jobs:
build:
name: npm-publish
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup node
uses: actions/setup-node#v2
with:
node-version: 14
- name: Install dependencies
run: npm install
- name: Build
run: npm run nx run-many -- --target=build --projects=ng-youtube-player-demo --configuration production --with-deps
- name: Test
run: npm run nx run-many -- --target=test --projects=ng-youtube-player-demo --with-deps --watch=false --browsers=ChromeHeadless --coverage --coverageReporters=lcovonly
- name: Upload code coverage report
uses: codecov/codecov-action#v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./coverage/libs/mintplayer-ng-youtube-api
- name: Upload code coverage report
uses: codecov/codecov-action#v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./coverage/libs/mintplayer-ng-youtube-player
- name: Push to NPM
uses: JS-DevTools/npm-publish#v1
with:
package: 'dist/libs/mintplayer-ng-youtube-api/package.json'
registry: 'https://registry.npmjs.org'
token: ${{ secrets.PUBLISH_NODE_TO_NPMJS_COM }}
access: 'public'
- name: Push to NPM
uses: JS-DevTools/npm-publish#v1
with:
package: 'dist/libs/mintplayer-ng-youtube-player/package.json'
registry: 'https://registry.npmjs.org'
token: ${{ secrets.PUBLISH_NODE_TO_NPMJS_COM }}
access: 'public'
- name: Push to Github
uses: JS-DevTools/npm-publish#v1
with:
package: 'dist/libs/mintplayer-ng-youtube-api/package.json'
registry: 'https://npm.pkg.github.com'
token: ${{ github.token }}
access: 'public'
- name: Push to Github
uses: JS-DevTools/npm-publish#v1
with:
package: 'dist/libs/mintplayer-ng-youtube-player/package.json'
registry: 'https://npm.pkg.github.com'
token: ${{ github.token }}
access: 'public'
npm test generates my code-coverage reports. The following step:
- name: Upload code coverage report
uses: codecov/codecov-action#v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./coverage/libs/mycompany-ng-youtube-player
uploads the coverage report to codecov.io. They will then analyse your report, and when finished, you can put a badge on your readme.md
[![codecov](https://codecov.io/gh/MyCompany/mycompany-ng-youtube-player/branch/master/graph/badge.svg?token=K0B355423X3)](https://codecov.io/gh/MyCompany/mycompany-ng-youtube-player)
Find this badge markdown on codecov.io → your repository → Settings → Badge → copy the markdown
this is what works for me:
1. Using danielpalme/ReportGenerator action for generating csv file
2. Read the csv file to the ci-cd
- name: Read Code Coverage
id: covergae
uses: juliangruber/read-file-action#v1
with:
path: Summary.csv
- name: Echo Code Coverage
run: echo "${{ steps.covergae.outputs.content }}"

How to upload xUnit coverage reports to Codecov?

I have a .Net 5 solution with multiple xUnit test projects which is a public repository hosted on Github. I would like to generate code coverage reports and display them on Codecov.
First I run
dotnet add package coverlet.msbuild
for each test project. I know that I can navigate to the .sln directory and generate a new report via
dotnet test /p:CollectCoverage=true
I authorized Codecov, so it knows about this project. I think only the main and dev branches are relevant so I started with this workflow
name: Generate coverage report on push
on:
push:
branches:
- 'main'
- 'dev'
jobs:
generate-coverage-report-on-push:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./DirectoryWithSlnFile
steps:
- uses: actions/checkout#v2
- name: Setup .NET
uses: actions/setup-dotnet#v1
with:
dotnet-version: 5.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build project
run: dotnet build --no-restore
- name: Generate coverage report
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true
What needs to be done now to upload the report from all test projects to Codecov? E.g.
- name: Upload coverage report
run: upload to Codecov with CODECOV_TOKEN if pushed on main or dev
I just stumbled upon this when having the same question. This is how I made it work.
First, the step you already had but just for completeness. Add coverlet to the test project(s):
dotnet add package coverlet.msbuild
Then, in the GitHub Actions workflow file, add the following steps:
- name: Test
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Codecov
uses: codecov/codecov-action#v2
with:
file: coverage.opencover.xml
directory: FUI.Tests

Categories

Resources