Tuesday, August 17

Running Tests for Multiple Pythons

For my Planar project, I'm committed to being compatible for Python 2.6-3.1 and beyond. Since this project contains C extensions, that means that I need to be able to build and test for each Python conveniently. And yes, different Pythons do behave differently, have different compilation gripes, and sometimes even expose different bugs due to internal differences in GC, etc.

This shell script I wrote is a simple, but effective solution. It requires that all of the versions of Python you are testing are installed (of course), and also have nose installed. It is very easy to extend to more Python versions as well by modifying the for loop at the top. You run it from the top-level source directory, alongside your setup.py. I imagine other folks have come up with more elegant solutions than this, but I figured it might be useful to someone, so here it is:
#!/bin/bash
# Build from scratch and run unit tests 
# from python 2.6, 2.7 and 3.1
# Then run doctests to verify doc examples
# Note: requires nose installed in each python instance

error=0

rm -rf build

for ver in 2.6 2.7 3.1; do
 echo "************"
 echo " Python $ver"
 echo "************"
 echo
 if which python${ver}; then
  python${ver} setup.py build && \
  python${ver} -m nose.core \
   -d -w build/lib.*${ver}/ --with-coverage $@ || error=1
 else
  echo >&2 "!!! Python ${ver} not found !!!"
  error=1
 fi
done

echo
echo -n "Doctests... "
srcdir=`pwd`
cd build/lib.*3.?/ && \
  python3 -m doctest ${srcdir}/doc/source/*.rst && \
  echo "OK" || error=1

exit $error

The bottom part runs doctests you might have in doc/source, which is where I keep the sphinx doc sources.