#!/usr/bin/env python """ app/ -> libs/ -> tests/ -> test libs/ -> __init__.py -> foo.py tests/ -> __init__.py -> foo.py tests/foo.py contents: import libs.foo """ # app/test: import sys import os import unittest def run_tests(): tests = sys.argv[1:] formatted = lambda s: s if not tests: tests = os.listdir('tests') formatted = lambda s: os.path.splitext(s)[0] tests = ['tests.%s' % formatted(test) for test in tests] tests = map(__import__, tests) load = unittest.defaultTestLoader.loadTestsFromModule return unittest.TestSuite(map(load, tests)) if __name__ == '__main__': try: unittest.main(defaultTest='run_tests') except AttributeError: print 'nonexistent test names' """ $ ./test foo Gives me 'nonexistent test names'. But if I call run_tests() manually, instead of unittest.main(defaultTest='run_tests') the same command ($ ./test foo) works without raising any exception, not an AttributeError, but not even an ImportError. """