【问题标题】:Test coverage in teamcity with Django使用 Django 在 teamcity 中测试覆盖率
【发布时间】:2011-05-25 20:02:32
【问题描述】:

我已经让 teamcity 工作,它构建并运行了一个自定义测试运行程序 (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 获取测试覆盖率数据。

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

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

【问题讨论】:

    标签: django continuous-integration teamcity django-testing


    【解决方案1】:

    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/

    【讨论】:

    • 您的 makefile 不正确。您可以通过运行“make -j 8”来检查它。 cover_report 应该依赖于cover_test,因为它需要在它之后运行。
    【解决方案2】:

    我在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
    

    然后,如果您将其添加到您的工件中,您可以添加一个包含覆盖 html 的选项卡:

    ./htmlcov/
    

    我还添加了一个带有行计数器的选项卡,您需要安装 cloc 或您选择的行计数器。

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

    +:**.pip
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      相关资源
      最近更新 更多