I'm setting up a pipeline for .NET 6 project on GitLab.
I run the test, then generate the code coverage report in the coverage.cobertura.xml
file.
Here is the test job script:
test:
only:
- master
- /^feature/.*$/
stage: test
dependencies:
- build-application
variables:
CONFIGURATION: "Debug"
COVERAGE_FLAG: "XPlat Code Coverage"
LOGGER_FLAG: "junit;LogFilePath=$CI_PROJECT_DIR/junit/junit-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
TEST_PROJECTS: "./tests/*Tests/*.csproj"
script:
- 'dotnet test $TEST_PROJECTS
-c $CONFIGURATION
-r $CI_PROJECT_DIR/cobertura
--collect:"$COVERAGE_FLAG"
--test-adapter-path:.
--logger:"$LOGGER_FLAG"'
- chmod x ./scripts/print-dotnet-coverage.sh
- ./scripts/print-dotnet-coverage.sh $CI_PROJECT_DIR/cobertura
coverage: /TOTAL_COVERAGE=(\d .\d )/
artifacts:
when: on_success
paths:
- $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml
- $CI_PROJECT_DIR/junit/junit-test-result.xml
reports:
coverage_report:
coverage_format: cobertura
path: $CI_PROJECT_DIR/cobertura/*/coverage.cobertura.xml
junit:
- $CI_PROJECT_DIR/junit/junit-test-result.xml
The pain is the test command doesn't produce any output concerning the total coverage percentage, so I extract it from the newly created coverage.cobertura.xml
file and print it to stdout using the following script
#!/usr/bin/env sh
REPORTS_DIR="${1}"
coverage=0
count=0
for i in $(find "$REPORTS_DIR" -name '*.xml');
do
printf "Found coverage report: %s\n" "$i"
coverage="$(xmllint --xpath 'string(/coverage/@line-rate)' ${i})"
count=$((count 1))
done;
printf "Found a total of %i report(s)\n" "$count"
coverage=$(echo "$coverage * 100" | bc) <-- error here
printf "TOTAL_COVERAGE=%2.4f\n" "$(echo "${coverage}")"
Because the coverage percentage is 0.8875, I have to multiply it by 100. But I got an error
./scripts/print-dotnet-coverage.sh: 12: bc: not found
Does anyone know how to fix this error or how to achieve my purpose - multiply the coverage by 100 in GitLab CI - without using bc?
Thank you!
CodePudding user response:
See the following example project for setting up code coverage reporting: dotnet-example