TeamCity 覆盖范围
在 TeamCity 中,我通过以下方式介绍 Django
make ci_test
通过调用命令使用创建覆盖率报告Makefile
。
VENV_PATH := $(HOME)/venv/bin
PROJ_NAME := my_awesome_project
# ...
ci_test: cover_test cover_report
cover_test:
$(VENV_PATH)/coverage run --source=$(PROJ_NAME) manage.py test -v 2 --noinput
cover_report:
$(VENV_PATH)/coverage report -m
$(VENV_PATH)/coverage html
$(VENV_PATH)/coverage-badge > htmlcov/coverage.svg
该cover_test
命令运行 Django 测试,并测量代码的覆盖率。该cover_report
命令将封面报告打印到控制台,并生成一个 html 报告,并使用coverage-badge实用程序生成一个带有徽章代码覆盖状态的漂亮徽章。
之后,将工件收集在 TeamCity(选项卡General Settings
)中
它们被收集在一个选项卡中Artifacts
并通过路径在 CI 服务器上可用:
/repository/download/%teamcity.project.id%/%teamcity.build.id%:id/htmlcov /index.html
/repository/download/%teamcity.project.id%/.lastFinished/htmlcov/index.html
GitHub 报告覆盖率
最后推送 GitHub 钩子以在 repo 中显示构建覆盖状态:
覆盖待定(构建前)
OWNER="<GITHUB OWNER>";
REPO="<REPO NAME>";
SHA="%build.vcs.number%";
curl "https://api.github.com/repos/$OWNER/$REPO/statuses/$SHA" \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: token <GITHUB API TOKEN>" \
-d '{
"state": "pending",
"description": "Coverage pending.",
"context": "continuous-integration/coverage"
}'
覆盖徽章副本
BADGE="/path/to/public/dir/badges/%teamcity.project.id%/%teamcity.build.branch%-coverage.svg"
DIR=$(dirname "${BADGE}")
mkdir -p $DIR
cp -f htmlcov/coverage.svg $BADGE
覆盖完成挂钩
OWNER="<GITHUB OWNER>";
REPO="<REPO NAME>";
SHA="%build.vcs.number%";
REPORT_URL="http://<YOU TEAMCITY DOMAIN>/repository/download/%teamcity.project.id%/%teamcity.build.id%:id/htmlcov/index.html";
COVERAGE=$(cat ./htmlcov/index.html | grep '<span class="pc_cov">' | grep -o '[0-9]\+');
if [ "$COVERAGE" -ge "85" ]; then
STATUS='success';
else
STATUS='failure';
fi
curl "https://api.github.com/repos/$OWNER/$REPO/statuses/$SHA" \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: token <GITHUB API TOKEN>" \
-d '{
"state": "'$STATUS'",
"target_url": "'$REPORT_URL'",
"description": "Coverage '$COVERAGE'%",
"context": "continuous-integration/coverage"
}'
github中的结果:
关于此的博客文章ru
:https ://maks.live/articles/drugoe/otchety-coverage-v-teamcity/