Skip to content

Instantly share code, notes, and snippets.

@mikejr83
Last active August 17, 2018 16:13
Show Gist options
  • Save mikejr83/0e3bc2fd9d2d5695c2aba0bdfd8d54c3 to your computer and use it in GitHub Desktop.
Save mikejr83/0e3bc2fd9d2d5695c2aba0bdfd8d54c3 to your computer and use it in GitHub Desktop.
Run Tests in .NET Core With Code Coverage from Coverlet
#!/bin/bash
# Coverage reports will be placed here...
COVERAGE_DIR="coverage-reports"
if [ -d "$COVERAGE_DIR" ]; then
rm -rf "$COVERAGE_DIR"
fi
# Build the solution so that the test command does not
echo "Cleaning and building solution..."
dotnet clean -v q && dotnet build -v q
# Create the directory for coverage reports because we don't want to
# assume that the command won't bomb if it isn't there.
mkdir -p "$COVERAGE_DIR"
# All the assemblies in the solution
ASSEMBLIES=$(ls */*.csproj | xargs -L1 dirname | sed -r 's/\.Tests//g')
# Go through each test project and run the test command with coverage.
for proj in *Tests/*.csproj; do
echo "Running Test Coverage for Project: $proj"
cd `dirname $proj`
CURRENT_NAME="`dirname $proj`"
DLL_NAME="${CURRENT_NAME/.Tests/}"
EXCLUDES_PARAMS=$(echo ASSEMBLIES | sed -r 's/'"${DLL_NAME//./\\.}"'//g' | sed -r 's/^\s?/\/p:Exclude=\"\[/g' | sed -r 's/(\s+$)|$/\]*\"/g' | sed -r 's/\s+/\]*\" \/p:Exclude=\"\[/g')
EXCLUDES_COMMA=$(echo ASSEMBLIES | sed -r 's/'"${DLL_NAME//./\\.}"'//g' | sed -r 's/(^\s?)/\[/g' | sed -r 's/\s+?$/\]*/g' | sed -r 's/\s+/]*,\[/g')
eval "MSYS_NO_PATHCONV=1 dotnet test --no-build /p:CollectCoverage=true /p:CoverletOutput=\"../$COVERAGE_DIR/$CURRENT_NAME.json\" /p:Exclude=\\\"$EXCLUDES_COMMA\\\" /p:Threshold=30"
cd ..
done
if [ ! -d "dotnet-tools" ]; then
dotnet tool install --tool-path=dotnet-tools dotnet-reportgenerator-globaltool --version 4.0.0-rc3
fi
REPORT_BUILDER_BIN="$(ls dotnet-tools/reportgenerator*)"
if [ ! -f $REPORT_BUILDER_BIN ]; then
dotnet tool install --tool-path=dotnet-tools dotnet-reportgenerator-globaltool --version 4.0.0-rc3
fi
eval "$REPORT_BUILDER_BIN \"-reports:coverage-reports/*Tests.xml\" \"-targetdir:coverage-reports\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment