views:

76

answers:

2

Hi,

What is the pythonic way of writing a unittest to see if a module is properly installed? By properly installed I mean, it does not raise an ImportError: No module named foo.

+1  A: 

I don't see why you'd need to test this, but something like:

def my_import_test(self):
    import my_module

If an import error is raised the test has failed, if not it passes.

Alex Gaynor
Maybe I am not clear in what I need. I want to test whether the import of a module is successful or not so I can see what modules are missing and need to be installed. So how can I use an self.assertUnequals... statement
DrDee
+5  A: 

As I have to deploy my Django application on a different server and it requires some extra modules I want to make sure that all required modules are installed.

This is not a unit test scenario at all.

This is a production readiness process and it isn't -- technically -- a test of your application.

It's a query about the environment. Ours includes dozens of things.

Start with a simple script like this. Add each thing you need to be sure exists.

try:
    import simplejson
except ImportError:
    print "***FAILURE: simplejson missing***"
    sys.exit( 2 )
sys.exit( 0 )

Just run this script in each environment as part of installation. It's not a unit test at all. It's a precondition for setup install.

S.Lott
Thanks so much, and sorry for using the wrong terminology, it obviously put a whole bunch of people on the wrong foot :)
DrDee