我正在GitLab上为。net 6项目设置一个管道。我运行测试,然后在coverage.cobertura.xml
文件中生成代码覆盖率报告。下面是测试作业脚本:
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
问题是test命令没有产生任何关于总覆盖率百分比的输出,所以我从新创建的coverage.cobertura.xml
文件中提取它,并使用以下脚本
将其打印到标准输出#!/usr/bin/env sh
REPORTS_DIR="${1}"
coverage=0
count=0
for i in $(find "$REPORTS_DIR" -name '*.xml');
do
printf "Found coverage report: %sn" "$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.4fn" "$(echo "${coverage}")"
因为覆盖率是0.8875,我必须乘以100。但是我得到了一个错误
./scripts/print-dotnet-coverage.sh: 12: bc: not found
有没有人知道如何解决这个错误或如何实现我的目的-在GitLab CI中乘以100的覆盖率-不使用bc?
谢谢!
查看下面的示例项目来设置代码覆盖率报告:dotnet-example
- .gitlab-ci.yml
- 测试作业输出