1. Ronald Oussoren
  2. pyobjc


pyobjc / Scripts / runPyObjCTests

The branch 'pyobjc-ancient' does not exist.
#!/usr/bin/env python
This file is part of PyObjC <http://pyobjc.sourceforge.net>

Run the PyObjC unittests (in an installed tree). This script is based on 
Scripts/allTestsTogether.py by Dinu Gherman.

Usage: runPyObjCTests [options]

    -q, --quiet: Minimal output
    -v, --verbose: Much output
    --include-qui-tests: Also run gui tests
    --exclude-gui-tests: Don't run gui tests
    -?, --help: Show usage information

Currently '--include-gui-tests' is default, the runner will select the most
appropriate default some time in the future.

import os
import sys
import glob
import unittest
from os.path import basename, dirname, splitext, join, expanduser, walk
from fnmatch import fnmatch
import getopt
import objc
import Foundation

USAGE='Usage: runPyObjCTests [-qv] [--enable-gui-tests|--disable-gui-tests]\n'
PYOBJC_ROOT = dirname(objc.__path__[0])

# Header printed before actually running the tests. Include information about
# expected failures in this text.
Running TestSuite for PyObjC %s
Using Python %s

NOTE: Loading the testcases takes some time.

Expected runtime warnings:
YYYY-MM-DD HH:MM:SS.SSS python[PID] *** -[NSBitmapImageRep init]: selector not recognized
YYYY-MM-DD HH:MM:SS.SSS python[PID] *** _NSAutoreleaseNoPool(): Object 0xABCDEFG of class NSInvocation autoreleased with no pool in place - just leaking
(Both should occur exactly once)

Expected test failures:
- FAIL: test_CheckNSInvoke (objc.test.test_ctests.CTests)
  This checks for a bug in Cocoa on MacOS X 10.2, 10.3
- FAIL: test_TestArrayCoding (objc.test.test_ctests.CTests)
  This checks for a bug/feature of Cocoa on MacOS X 10.2, should not 
  occur on MacOS X 10.3 and later.
"""%(objc.__version__, sys.version)

def recursiveGlob(root, pathPattern):
    Recursively look for files matching 'pathPattern'. Return a list
    of matching files/directories.
    result = []

    def walker(data, dirname, files):
        for fn in files:
            if fnmatch(fn, data[0]):
                data[1].append(join(dirname, fn))

    walk(root, walker, (pathPattern, result))
    return result

def importExternalTestCases(pathPattern="test_*.py", root="."):
    Import all unittests in the PyObjC tree starting at 'root'

    testFiles = recursiveGlob(root, pathPattern)
    testModules = map(lambda x:x[len(root)+1:-3].replace('/', '.'), testFiles)

    suites = []
    for modName in testModules:
        module = __import__(modName)
        if '.' in modName:
            for elem in modName.split('.')[1:]:
                module = getattr(module, elem)

        s = unittest.defaultTestLoader.loadTestsFromModule(module)

    return unittest.TestSuite(suites)

def main(args=None):
    if args is None:
        args = sys.argv[1:]

    # TODO: automaticly set the right default
    include_gui = 1
    verbosity = 1

        opts, args = getopt.getopt(args, '?qv', 
                ['help', 'enable-gui-tests', 
                 'disable-gui-tests', 'quiet', 'verbose'])
    except getopt.error, msg:
        sys.stderr.write('runPyObjCTests: %s\n'%(msg,))

    if args:
        sys.stderr.write('runPyObjCTests: Extra arguments\n')

    for key, value in opts:
        if key in ['--enable-gui-tests']:
            include_gui = 1
        elif key in ['--disable-gui-tests']:
            include_gui = 0
        elif key in ['-?', '--help']:
            print USAGE
        elif key in ['-v', '--verbose']:
            verbosity = 2
        elif key in ['-q', '--quiet' ]:
            verbosity = 0

    if verbosity:

    plain_suite = importExternalTestCases("test_*.py", PYOBJC_ROOT)

    if include_gui:
        gui_suite = importExternalTestCases("guitest_*.py", PYOBJC_ROOT)
        suite = unittest.TestSuite((plain_suite, gui_suite))
        suite = unittest.TestSuite((plain_suite,))

    runner = unittest.TextTestRunner(verbosity=verbosity)

if __name__ == "__main__":