pyobjc / Scripts / runPyObjCTests

The branch 'pyobjc' 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]

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.
HEADER="""\
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)
        suites.append(s)

    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

    try:
        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,))
        sys.stderr.write('\n')
        sys.stderr.write(USAGE)
        sys.exit(1)

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

    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
            sys.exit(0)
        elif key in ['-v', '--verbose']:
            verbosity = 2
        elif key in ['-q', '--quiet' ]:
            verbosity = 0

    if verbosity:
        sys.stdout.write(HEADER)

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

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

    runner = unittest.TextTestRunner(verbosity=verbosity)
    runner.run(suite)

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.