#!/usr/bin/env python

import os
import sys
_HERE = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.abspath(os.path.join(_HERE, os.pardir)))


import collections
import optparse
import os
import subprocess
import sys
import unittest


def _find_tests(path, selectors=None):
    if selectors is None:
        def filter(test):
            return True
    else:
        def filter(test):
            return any(selector in _get_test_full_name(test)
                       for selector in selectors)

    out = []
    stack = collections.deque((unittest.TestLoader().discover(path),))
    while stack:
        obj = stack.popleft()
        if isinstance(obj, unittest.TestSuite):
            stack.extend(test for test in obj)
        elif type(obj).__name__ == 'ModuleImportFailure':
            try:
                # This should always throw an ImportError exception.
                getattr(obj, _get_test_name(obj))()
            except ImportError as e:
                sys.exit(e.message.strip())
        elif filter(obj):
            out.append(obj)

    return out


def _get_test_name(test):
    return test._testMethodName


def _get_test_full_name(test):
    return '%s.%s.%s' % (test.__class__.__module__, test.__class__.__name__,
                         _get_test_name(test))


def main():
    usage = "usage: %prog [options] [test1..testN]"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option(
        '-s', '--split', action='store_true', dest='split',
        help="run each test in a separate subprocess"
    )

    options, args = parser.parse_args()

    selectors = args if args else None
    tests = _find_tests(_HERE, selectors)

    if options.split:
        for test in tests:
            name = _get_test_full_name(test)
            subprocess.call([sys.executable, '-m', 'unittest', '-v', name],
                            env={'PYTHONPATH': ':'.join(sys.path)})
    else:
        suite = unittest.TestLoader().suiteClass(tests)
        unittest.TextTestRunner(verbosity=2).run(suite)


if __name__ == "__main__":
    main()

@@ -193,26 +193,16 @@
 ptparse%0A
-import os%0A
 import s

Remove unused module import