Atlassian Bamboo with Django & Python - Possible? Atlassian Bamboo with Django & Python - Possible? python python

Atlassian Bamboo with Django & Python - Possible?


Bamboo essentially just runs a shell script, so this could just as easily be:

./manage.py test

as it typically is:

mvn clean install

or:

ant compile

You may have to massage to output of the Django test runner into traditional JUnit XML output, so that Bamboo can give you pretty graphs on how many tests passed. Look at this post about using xmlrunner.py to get Python working with Hudson. Also take a look at NoseXUnit.


You can even add a bootstrap for pip and virtualenv on a clean environment quite easily, which is cool:

wget https://bootstrap.pypa.io/get-pip.pypython get-pip.py --root=${bamboo.build.working.directory}/tmp --ignore-installedexport PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATHexport PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATHpip install --root=${bamboo.build.working.directory}/tmp --ignore-installed virtualenvvirtualenv virtual_tmpcd virtual_tmp. bin/activateecho Pip is located `which pip`pip install djangopip install djangorestframework

Warning, source bin/activate does not work as the inline script tasks are stored into an sh file (so bash run it in sh compatibility mode).

Edit

Even better, we can run unit tests on the top of it, with xml outputs that can be parsed by the JUnit of bamboo:

pip install unittest-xml-reportingpython manage.py test --noinput --testrunner="xmlrunner.extra.djangotestrunner.XMLTestRunner"


It turns out it is possible. There are two major integration tasks: test runner results and code coverage results. I assume normal Python 3 codebase and standard unittest test suite.

Test runner

Bamboo expects test runner results in JUnit XML format. There is separate test runner on the Cheese Shop able to produce such output, but it would require you to write a little code to run it, which is not nice. Better way which keeps the codebase intact is to use pytest's features.

Code coverage

Bamboo only supports the XML format of Atlassian Clover. Important note here is that you don't need Atlassian Clover plugin enabled (and license for it which costs some bucks). Bamboo works on its own.

Python de facto standard code coverage tool, coverage, produces somewhat Cobertura XML format, but there's a converter. There's a pytest plugin for integration with the coverage tool.

Solution

Here's the Tox environment where I used pytest to make both Bamboo integrations work.

[tox]envlist   = py34skipsdist = True[testenv]setenv     = LANG=C.UTF-8basepython = python3.4deps       = -r{toxinidir}/requirements.txt[testenv:bamboo]commands =   py.test --junitxml=results.xml \    --cov=project_name --cov-config=tox.ini --cov-report=xml \    --cov-report=html project_name/test    coverage2clover -i coverage.xml -o clover.xmldeps =     {[testenv]deps}    pytest    pytest-cov    coverage2clover# read by pytest[pytest]python_files = *.py# read by coverage[run]omit=project_name/test/*,project_name/__main__.py

Note that both pytest and pytest-cov use tox.ini for the configuration that is not supported on command line. It again saves your from having additional clutter in root of your repo. pytest tries to read tox.ini automatically. pytest-cov bypasses to .coveragerc, but because it's also an INI file, tox.ini fits.

On Bamboo side add a script task that runs tox -e bamboo. Then add JUnit parse task to the job. In its dialogue, under Specify custom results directories put results.xml.

Coverage configuration is done other way.

  1. Open Miscellaneous tab of your job
  2. Check Use Clover to collect Code Coverage for this build
  3. Select Clover is already integrated into this build and a clover.xml file will be produced
  4. Type clover.xml into Clover XML Location

enter image description here

At this point in your next build you will see total coverage and two charts: Coverage history and Lines of code history. It's also nice to have interactive HTML produced by coverage tool, so you can drill down to certain line of code.

The settings made above (at least in Bamboo 5.7) has created Clover Report (System) in Artifact job's tab. Open it and set htmlcov to Location field, and *.* to Copy pattern. Bamboo will now collect the HTML reports. You can see it at Clover tab of your plan.