Skip to content
Phillip Cloud edited this page Aug 9, 2013 · 10 revisions

Python

Testing for versions

In [1]: from distutils.version import LooseVersion
In [2]: LooseVersion(np.__version__)>='1.7.0'
Out[2]: True

Skipping an entire file full of tests on a failed import of a module named a_module

import nose

try:
    import a_module
except ImportError:
    raise nose.SkipTest("message why this test is being skipped")

Note: We implore you to write a message in the raise nose.SkipTest statement/expression so that when testing we can see why things were skipped without having to go back and look at the code. See below for a way to print out skipped tests after running nosetests.

Nose

Exclude a particular test (e.g., a test that's failing you don't care about)

nosetests -Isome_test_name

Fail fast (on first failure - useful with git bisect)

nosetests -x

Run with ids (and just re-run tests that failed)

# stores ids
nosetests --with-ids
# runs only tests that previously failed 
nosetests --failed

Print out skipped tests and their messages

nosetests -w pandas --with-xunit --xunit-file=/tmp/nosetests.xml
ci/print_skipped.py /tmp/nosetests.xml

Misc

Reading from a url

from pandas.io.common import urlopen
with urlopen('http://www.google.com') as url:
    raw_text = url.read()

Reading a file named file.txt that's inside of a zip file named file.zip

from pandas.io.common import ZipFile
with ZipFile('file.zip') as zf:
    raw_text = zf.read('file.txt')