使用Django测试teamcity中的覆盖范围

时间:2010-12-15 10:24:32

标签: django continuous-integration teamcity django-testing

我有团队合作,它构建并运行自定义testrunner(http://pypi.python.org/pypi/teamcity-messages

我愚蠢地愚弄了这篇文章:TeamCity for Python/Django continuous integration

我的run_suite方法如下所示:

from teamcity import underTeamcity
from teamcity.unittestpy import TeamcityTestRunner
return TeamcityTestRunner().run(suite)

我目前使用django_coverage和coverage.py,我希望teamcity获取测试覆盖率数据。

我不喜欢团队城市,但我更喜欢将它用作CI服务器,但如果更容易,我可以改为另一个。

如何获取./manage.py test_coverage在teamcity中打印的数据?

2 个答案:

答案 0 :(得分:1)

我在settings.py中使用teamcity-nose以下配置:

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

执行测试的构建步骤如下所示:

. /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

如果将其添加到工件中,则可以添加包含coverage html的选项卡:

./htmlcov/

我也添加了一个带行计数器的标签,你需要安装cloc或你选择的行计数器。

我还有一个额外的构建配置,可以每晚通过fab部署到登台服务器(只需像往常一样激活和制作),以及通过将pip文件更改为自动安装pip要求的额外构建,将其添加到触发器中“pip install -r requirements.pip”build的规则:

+:**.pip

我将此添加到我的测试版本中,以便当pip和其他一些文件发生更改而不影响构建测试时它不会运行:

+:.
-:**.pip
-:*fabfile.py
-:*myproj/conf/*
+:*myproj/conf/teamcity/*

答案 1 :(得分:1)

TeamCity Coverage

在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实用程序生成徽章代码覆盖状态为badge.svg的漂亮徽章。

之后,在TeamCity(选项卡General Settings

中收集工件

teamcity conf

它们收集在标签Artifacts

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中的结果:

pending

fail

pass

关于此ru的博文:https://maks.live/articles/drugoe/otchety-coverage-v-teamcity/