Index: run.py
===================================================================
--- run.py	(revision 2f2d9d06d2bf050db011fe7fdf384273f9bdc61f)
+++ run.py	(revision bbd97e5cc51780c7e00fb5c22b4c92799a7c0216)
@@ -15,19 +15,7 @@
 import imp
 from glob import glob
+from contextlib import contextmanager
+from os.path import abspath, dirname, join as joinpath
 
-# find the directories for the source and build
-from distutils.util import get_platform
-root = os.path.abspath(os.path.dirname(__file__))
-platform = '%s-%s'%(get_platform(),sys.version[:3])
-build_path = os.path.join(root, 'build','lib.'+platform)
-
-# Make sure that we have a private version of mplconfig
-mplconfig = os.path.join(os.getcwd(), '.mplconfig')
-os.environ['MPLCONFIGDIR'] = mplconfig
-if not os.path.exists(mplconfig): os.mkdir(mplconfig)
-#import matplotlib
-#matplotlib.use('Agg')
-#print matplotlib.__file__
-#import pylab; pylab.hold(False)
 
 def addpath(path):
@@ -36,5 +24,5 @@
     environment variable for subprocesses.
     """
-    path = os.path.abspath(path)
+    path = abspath(path)
     if 'PYTHONPATH' in os.environ:
         PYTHONPATH = path + os.pathsep + os.environ['PYTHONPATH']
@@ -44,5 +32,4 @@
     sys.path.insert(0, path)
 
-from contextlib import contextmanager
 @contextmanager
 def cd(path):
@@ -57,7 +44,7 @@
 def import_package(modname, path):
     """Import a package into a particular point in the python namespace"""
-    mod = imp.load_source(modname, os.path.abspath(os.path.join(path,'__init__.py')))
+    mod = imp.load_source(modname, abspath(joinpath(path,'__init__.py')))
     sys.modules[modname] = mod
-    mod.__path__ = [os.path.abspath(path)]
+    mod.__path__ = [abspath(path)]
     return mod
 
@@ -65,64 +52,78 @@
     """Import a DLL from the build directory"""
     # build_path comes from context
-    path = glob(os.path.join(build_path, *modname.split('.'))+'.*')[0]
+    path = glob(joinpath(build_path, *modname.split('.'))+'.*')[0]
     #print "importing", modname, "from", path
     return imp.load_dynamic(modname, path)
 
+def prepare():
+    # Don't create *.pyc files
+    sys.dont_write_bytecode = True
 
-# Don't create *.pyc files
-sys.dont_write_bytecode = True
+    # Debug numpy warnings
+    #import numpy; numpy.seterr(all='raise')
 
-# Debug numpy warnings
-#import numpy; numpy.seterr(all='raise')
+    # find the directories for the source and build
+    from distutils.util import get_platform
+    root = abspath(dirname(__file__))
+    platform = '%s-%s'%(get_platform(),sys.version[:3])
+    build_path = joinpath(root, 'build','lib.'+platform)
 
-# add periodictable to the path
-try: import periodictable
-except: addpath(os.path.join(root, '..','periodictable'))
+    # Make sure that we have a private version of mplconfig
+    mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')
+    os.environ['MPLCONFIGDIR'] = mplconfig
+    if not os.path.exists(mplconfig): os.mkdir(mplconfig)
+    #import matplotlib
+    #matplotlib.use('Agg')
+    #print matplotlib.__file__
+    #import pylab; pylab.hold(False)
+    # add periodictable to the path
+    try: import periodictable
+    except: addpath(joinpath(root, '..','periodictable'))
 
-# select wx version
-#addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
+    # select wx version
+    #addpath(os.path.join(root, '..','wxPython-src-3.0.0.0','wxPython'))
 
-# Build project if the build directory does not already exist.
-if not os.path.exists(build_path):
-    import subprocess
-    with cd(root):
-        subprocess.call((sys.executable, "setup.py", "build"), shell=False)
+    # Build project if the build directory does not already exist.
+    if not os.path.exists(build_path):
+        import subprocess
+        with cd(root):
+            subprocess.call((sys.executable, "setup.py", "build"), shell=False)
 
-# Put the source trees on the path
-addpath(os.path.join(root, 'src'))
-addpath(os.path.join(root, 'park-1.2.1'))
+    # Put the source trees on the path
+    addpath(joinpath(root, 'src'))
+    addpath(joinpath(root, 'park-1.2.1'))
 
-# Import the sansview package from root/sansview as sans.sansview.  It would
-# be better to just store the package in src/sans/sansview.
-import sans
-sans.sansview = import_package('sans.sansview', os.path.join(root,'sansview'))
+    # Import the sansview package from root/sansview as sans.sansview.  It would
+    # be better to just store the package in src/sans/sansview.
+    import sans
+    sans.sansview = import_package('sans.sansview', joinpath(root,'sansview'))
 
-# The sans.models package Compiled Model files should be pulled in from the build directory even though
-# the source is stored in src/sans/models.
+    # The sans.models package Compiled Model files should be pulled in from the build directory even though
+    # the source is stored in src/sans/models.
 
-# Compiled modules need to be pulled from the build directory.
-# Some packages are not where they are needed, so load them explicitly.
-import sans.pr
-sans.pr.core = import_package('sans.pr.core',
-                              os.path.join(build_path, 'sans', 'pr', 'core'))
-#import_dll('park._modeling')
+    # Compiled modules need to be pulled from the build directory.
+    # Some packages are not where they are needed, so load them explicitly.
+    import sans.pr
+    sans.pr.core = import_package('sans.pr.core',
+                                  joinpath(build_path, 'sans', 'pr', 'core'))
+    #import_dll('park._modeling')
 
-#park = import_package('park',os.path.join(build_path,'park'))
+    #park = import_package('park',os.path.join(build_path,'park'))
 
-# Pull the entire sans.models package from the build directory since it contains
-# a mixture of autogenerated python and C.  Any changes in models will require
-# a rebuild with setup.py build
-sans.models = import_package('sans.models', os.path.join(build_path, 'sans', 'models'))
+    # Pull the entire sans.models package from the build directory since it contains
+    # a mixture of autogenerated python and C.  Any changes in models will require
+    # a rebuild with setup.py build
+    sans.models = import_package('sans.models', joinpath(build_path, 'sans', 'models'))
 
-sys.path.append(build_path)
+    sys.path.append(build_path)
 
-#print "\n".join(sys.path)
-#from sans.models import SphereModel
+    #print "\n".join(sys.path)
+    #from sans.models import SphereModel
 
-# Won't need freeze support when running from the source tree
-#import multiprocessing
-#multiprocessing.freeze_support()
-
-# start sasview
-from sans.sansview.sansview import SasView
-SasView()
+if __name__ == "__main__":
+    # start sasview
+    #import multiprocessing
+    #multiprocessing.freeze_support()
+    prepare()
+    from sans.sansview.sansview import SasView
+    SasView()
Index: test/run_one.py
===================================================================
--- test/run_one.py	(revision bbd97e5cc51780c7e00fb5c22b4c92799a7c0216)
+++ test/run_one.py	(revision bbd97e5cc51780c7e00fb5c22b4c92799a7c0216)
@@ -0,0 +1,20 @@
+import os
+import sys
+import xmlrunner
+import unittest
+import imp
+from os.path import abspath, dirname, split as splitpath, join as joinpath
+
+run_py = joinpath(dirname(dirname(abspath(__file__))), 'run.py')
+#print run_py
+run = imp.load_source('sasview_run', run_py)
+run.prepare()
+#print "\n".join(sys.path)
+test_path,test_file = splitpath(sys.argv[1])
+#print test_path, test_file
+sys.argv = [sys.argv[0]]
+os.chdir(test_path)
+sys.path.insert(0, test_path)
+test = imp.load_source('tests',test_file)
+unittest.main(test, testRunner=xmlrunner.XMLTestRunner(output='logs'))
+
Index: test/utest_sansview.py
===================================================================
--- test/utest_sansview.py	(revision 21b7a4932db1789027ff270369adb3d1062cd108)
+++ test/utest_sansview.py	(revision bbd97e5cc51780c7e00fb5c22b4c92799a7c0216)
@@ -8,4 +8,5 @@
     print "xmlrunner needs to be installed to run these tests"
     print "Try easy_install unittest-xml-reporting"
+    sys.exit(1)
 
 # Check whether we have matplotlib installed
@@ -21,9 +22,11 @@
     SKIPPED_DIRS.append("sansguiframe")
 
-COMMAND_SEP = ';'
-if os.name == 'nt':
-    COMMAND_SEP = '&'
+#COMMAND_SEP = ';'
+#if os.name == 'nt':
+#    COMMAND_SEP = '&'
 
 def run_tests():
+    test_root = os.path.abspath(os.path.dirname(__file__))
+    run_one_py = os.path.join(test_root, 'run_one.py')
     passed = 0
     failed = 0
@@ -32,5 +35,5 @@
     n_failures = 0
     
-    for d in os.listdir(os.getcwd()):
+    for d in os.listdir(test_root):
         
         # Check for modules to be skipped
@@ -39,5 +42,5 @@
         
         # Go through modules looking for unit tests
-        module_dir = os.path.join(os.getcwd(),d,"test")
+        module_dir = os.path.join(test_root, d, "test")
         if os.path.isdir(module_dir):
             for f in os.listdir(module_dir):
@@ -45,9 +48,9 @@
                 if os.path.isfile(file_path) and f.startswith("utest_") and f.endswith(".py"):
                     module_name,_ = os.path.splitext(f)
-                    code = "cd %s%s%s -c \"import sys;import xmlrunner;import unittest;sys.path.insert(0, '%s');" % (module_dir, COMMAND_SEP, sys.executable, module_dir)
-                    code += "from %s import *;" % module_name
-                    code += "unittest.main(testRunner=xmlrunner.XMLTestRunner(output='logs'))\""
+                    code = '"%s" %s %s'%(sys.executable, run_one_py, file_path)
                     proc = subprocess.Popen(code, shell=True, stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
                     std_out, std_err = proc.communicate()
+                    #print std_out
+                    #sys.exit()
                     has_failed = True
                     m = re.search("Ran ([0-9]+) test", std_out)
