通过Django在teamcity中测试覆盖率
我让teamcity工作,它构建并运行一个定制的testrunner() 我愚蠢地愚弄了这篇文章: 我的run_suite方法如下所示:通过Django在teamcity中测试覆盖率,django,continuous-integration,teamcity,django-testing,Django,Continuous Integration,Teamcity,Django Testing,我让teamcity工作,它构建并运行一个定制的testrunner() 我愚蠢地愚弄了这篇文章: 我的run_suite方法如下所示: from teamcity import underTeamcity from teamcity.unittestpy import TeamcityTestRunner return TeamcityTestRunner().run(suite) . /opt/teamcity/virtualenvs/myproj/bin/activate dropdb
from teamcity import underTeamcity
from teamcity.unittestpy import TeamcityTestRunner
return TeamcityTestRunner().run(suite)
. /opt/teamcity/virtualenvs/myproj/bin/activate
dropdb test_myproj-teamcity &> /dev/null # bug that is not destroying database
manage.py test
我目前在coverage.py中使用django_覆盖率,我希望teamcity能够获得测试覆盖率数据
我不一定要加入teamcity,但我更喜欢将其用作CI服务器,但如果更容易的话,我可以换成另一个
如何获取./manage.py test\u coverage在teamcity中打印的数据?我使用settings.py中的以下配置:
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['-v', '-s', '--rednose', '--with-selenium',]
if os.getenv('TEAMCITY_PROJECT_NAME') is not None:
# whatever special teamcity settings you might have go here
进行测试的构建步骤如下所示:
from teamcity import underTeamcity
from teamcity.unittestpy import TeamcityTestRunner
return TeamcityTestRunner().run(suite)
. /opt/teamcity/virtualenvs/myproj/bin/activate
dropdb test_myproj-teamcity &> /dev/null # bug that is not destroying database
manage.py test
我的项目的manage.py位于路径上(我通过setup.py安装在virtualenv bin中)
因此,如果不这样做,则必须添加路径
我从未设法在测试本身中添加覆盖率,因为包版本控制存在问题,所以对于最新的覆盖率包,我只是在额外的构建步骤中添加了它:
. /opt/teamcity/virtualenvs/myproj/bin/activate
coverage html --include=myproj/*.*
cloc . --out=./htmlcov/cloc.txt
如果将其添加到工件中,则可以添加包含覆盖率html的选项卡:
./htmlcov/
我还添加了一个带有行计数器的选项卡,您需要安装cloc或您选择的行计数器
我还有一个额外的构建配置,用于每晚通过fab部署到临时服务器一次(只需像往常一样激活和fab),以及一个额外的构建,用于在pip文件更改时自动安装pip需求,方法是将此添加到“pip安装-r需求.pip”构建的触发规则中:
我将其添加到我的测试构建中,这样当pip和其他一些文件更改时,它就不会运行,而不会影响构建测试:
+:.
-:**.pip
-:*fabfile.py
-:*myproj/conf/*
+:*myproj/conf/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\u test
命令运行Django测试,并测量代码的覆盖率。cover\u report
命令将cover报告打印到控制台,还生成html报告,并使用该实用程序生成带有徽章代码覆盖状态的漂亮徽章
之后,在TeamCity中收集工件(选项卡常规设置)
它们收集在选项卡工件中
并可通过以下路径在CI服务器上使用:
/repository/download/%teamcity.project.id%/%teamcity.build.id%:id/htmlcv/index.html
/repository/download/%teamcity.project.id%/.lastFinished/htmlcov/index.html
GitHub报告覆盖率
最后,按下GitHub钩子以在repo中显示构建覆盖率状态:
覆盖率待定(生成前)
覆盖饰面挂钩
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"
}'
OWNER=”“;
回购=”;
SHA=“%build.vcs.number%”;
报告URL=”http:///repository/download/%teamcity.project.id%/%teamcity.build.id%:id/htmlcov/index.html";
覆盖率=$(cat./htmlcov/index.html | grep'| grep-o'[0-9]\+');
如果[“$COVERAGE”-ge“85”];然后
地位"成功",;
其他的
状态为“失败”;
fi
卷曲“https://api.github.com/repos/$OWNER/$REPO/status/$SHA”\
-X柱\
-H“内容类型:应用程序/json”\
-H“授权:令牌”\
-d'{
“状态”:“$STATUS”,
“目标url”:“$REPORT\u url”,
“说明”:“覆盖范围”$Coverage“%”,
“上下文”:“持续集成/覆盖”
}'
导致github:
关于此ru
:您的makefile不正确。您可以通过运行“make-j 8”进行检查。cover_报告应该依赖于cover_测试,因为它需要在它之后运行。
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"
}'