Test coverage in teamcity with Django

I have a teamcity team and it builds and runs its own test server ( http://pypi.python.org/pypi/teamcity-messages )

I cheated this post badly: TeamCity for continuous Python / Django integration

My run_suite method is as follows:

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

I am currently using django_coverage called cover.py, and I would like teamcity to get coverage data for testing.

I am not tied to teamcity, but I prefer to use it as a CI server, but I can switch to another if it is easier.

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

+3
source share
2 answers

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, which performs the testing, is as follows:

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

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

I never managed to add coverage to the test itself, because there are problems with version control of the packages, so with the last coverage package I just added it to an additional build step:

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

Then you can add a tab that includes html coverage if you add this to your artifact:

./htmlcov/

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

fab ( , ), , , "pip install -r requirements.pip" build:

+:**.pip

, , :

+:.
-:**.pip
-:*fabfile.py
-:*myproj/conf/*
+:*myproj/conf/teamcity/*
+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, 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 :

( )

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/

+1

Source: https://habr.com/ru/post/1780396/


All Articles