[TIP] Multiplatform testing

Mark Sienkiewicz sienkiew at stsci.edu
Tue Jan 4 14:31:29 PST 2011


> I solve this problem like this:
> 
> import unittest2
> try:
>     import module
> except ImportError:
>     module = None

I recommend explicitly recognizing the system where you don't expect the the test to work.  For example,

import platform
if platform.system() not in ( 'Windows', 'other_platform_to_skip' ) :
    import module
else :
    module = None

The helps insulate your tests against import errors that happen for some reason other than the one you expect.  If something goes wrong with your test environment, the ImportError causes the test run to error instead of quietly allowing a bunch of tests to switch their status to "skipped".

Mark S.



More information about the testing-in-python mailing list