Atlassian Bamboo with Django & Python - Possible?
Asked Answered
V

4

37

At my company, we currently use Atlassian Bamboo for our continuous integration tool. We currently use Java for all of our projects, so it works great.

However, we are considering using a Django + Python for one of our new applications. I was wondering if it is possible to use Bamboo for this.

First off, let me say that I have a low level of familiarity with Bamboo, as I've only ever used it, not configured it (other than simple changes like changing the svn checkout directory for a build).

Obviously there isn't a lot of point in just running a build (since Python projects don't really build), but I'd like to be able to use Bamboo for running the test suite, as well as use bamboo to deploy the latest code to our various test environments the way we do with our Java projects.

Does Bamboo support this type of thing with a Python project?

Vito answered 14/9, 2009 at 4:4 Comment(3)
Old question. But I have added py.test as a Bamboo executable and simply use this to run the tests (with junit-xml output specified). Then the JUnit test parser needs to be run as a separate job.Bertina
@SamuelO'Malley can you post an example of how you got that to work?Mesothelium
@Mesothelium - I have actually changed how it is done since this comment was posted. Now I was a Bamboo Script Task and have the contents as py.test --junit-xml results.xml and after that I have if [ $? -ne 0 ]; then echo "Test Failed"; fi to swallow the return code. Otherwise Bamboo won't allow you to quarantine an individual test result and will mark the whole Job as failed.Bertina
L
27

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.

Levona answered 14/9, 2009 at 4:51 Comment(0)
H
16

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.py
python get-pip.py --root=${bamboo.build.working.directory}/tmp --ignore-installed
export PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATH
export PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
pip install --root=${bamboo.build.working.directory}/tmp --ignore-installed virtualenv
virtualenv virtual_tmp
cd virtual_tmp
. bin/activate
echo Pip is located `which pip`
pip install django
pip 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-reporting
python manage.py test --noinput --testrunner="xmlrunner.extra.djangotestrunner.XMLTestRunner"
Homely answered 4/9, 2014 at 13:49 Comment(2)
@Raffi, can you elaborate a bit more on why source bin/activate does not work in file scripts? Been using it fine in inline scripts, but would like to move as much as possible to file scripts since it makes easier to maintain (version control, etc).Berube
In the build log of Bamboo and for inline scripts, I have something like Beginning to execute external process for build 'XXX Django artifactsZZZ #35'\n ... running command line: \n/bin/sh /tmp/SW-DCD-JOB1-35-ScriptBuildTask-9100354988645888838.sh. The inline script is flushed into a .sh file and /bin/sh is executed. sh does not have source builtin command (type source returns an error). So . is more compatible with what I want hereHomely
S
9

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   = py34
skipsdist = True

[testenv]
setenv     = LANG=C.UTF-8
basepython = python3.4
deps       = -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.xml
deps = 
    {[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.

Snuff answered 15/9, 2016 at 10:54 Comment(0)
W
0

If you use pytest you can simply use py.test --junitxml=/path/to/results/xml/file.xml

Whallon answered 13/7, 2015 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.