使用 Django 在 teamcity 中测试覆盖率

发布于 2024-10-07 21:05:49 字数 710 浏览 3 评论 0原文

我已经让 teamcity 工作了,它构建并运行了一个自定义测试运行程序(http://pypi.python. org/pypi/teamcity-messages

我粗略地愚弄了这篇文章:TeamCity for Python/Django 持续集成

我的 run_suite 方法如下所示:

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

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

我不受 teamcity 的约束,但我更喜欢将它用作 CI 服务器,但如果更容易的话我可以更改为另一个服务器。

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

I have got teamcity working and it builds and runs a custom testrunner (http://pypi.python.org/pypi/teamcity-messages)

I loosly foolowed this post: TeamCity for Python/Django continuous integration

My run_suite method looks like this:

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

I currently use django_coverage with coverage.py and I would like teamcity to get the test coverage data.

I am not bound to teamcity but I prefere using it as a CI server but I can change to another if it is easier.

How can I get the data that ./manage.py test_coverage prints in teamcity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-10-14 21:05:49

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

之后,工件将收集在 TeamCity 中(选项卡常规设置

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 hook在仓库中显示构建覆盖率状态:

覆盖率待定(构建前)

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

失败

 pass

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

TeamCity Coverage

In TeamCity I cover Django in the following way

Create coverage report by call make ci_test command use 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

The cover_test command runs the Django tests, and measures the coverage of the code. The cover_report command prints a cover report to the console, and also generates an html report and, using the coverage-badge utility, generates a beautiful badge with the badge code coverage status badge.svg.

After that, artifacts are collected in the TeamCity (tab General Settings)

teamcity conf

They are collected in a tab Artifacts

artifacts

And available on CI server by path:

  • /repository/download/%teamcity.project.id%/%teamcity.build.id%:id/htmlcov /index.html
  • /repository/download/%teamcity.project.id%/.lastFinished/htmlcov/index.html

GitHub report coverage

Finally push GitHub hook to display build coverage state in repo:

Coverage Pending (before build)

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"
    }'

Coverage badge copy

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

Coverage Finish hook

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"
    }'

Result in github:

pending

fail

pass

Blog post about this ru: https://maks.live/articles/drugoe/otchety-coverage-v-teamcity/

红焚 2024-10-14 21:05:49

我使用 teamcity-nose 并在 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

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

. /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 install -rrequirements.pip”构建:

+:**.pip

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

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

I use teamcity-nose with the following configuration in 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

My build step that does the testing looks like this:

. /opt/teamcity/virtualenvs/myproj/bin/activate
 dropdb test_myproj-teamcity &> /dev/null # bug that is not destroying database
 manage.py test

My project's manage.py is on the path (I install in the virtualenv bin via setup.py)
so you will have to add the path if you do otherwise.

I never managed to add the coverage in the test itself as there are problems with package versioning, so with the latest coverage package i Just added it in an extra build step:

. /opt/teamcity/virtualenvs/myproj/bin/activate
 coverage html --include=myproj/*.*
 cloc . --out=./htmlcov/cloc.txt

You can then add a tab that includes the coverage html if you add this to your artifact:

./htmlcov/

I also add a tab with the line counter too, you will need to have cloc or the linecounter of your choice installed.

I also have an extra build configuration for deploying to staging server once a night via fab (just activate and fab as usual), and an extra build for automatically installing pip requirements if the pip files change, by adding this to the triggering rules for the "pip install -r requirements.pip" build:

+:**.pip

And I add this to my test build so that it doesn't run when pip and some other files change that don't effect the build test:

+:.
-:**.pip
-:*fabfile.py
-:*myproj/conf/*
+:*myproj/conf/teamcity/*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文