testing.py :  » Network » Grail-Internet-Browser » grail-0.6 » utils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Grail Internet Browser 
Grail Internet Browser » grail 0.6 » utils » testing.py
"""Handy dandy routines for exercising yer code.

Use exercise() to try things that shouldn't fail, or things that should
fail with a known exception, and raise an exception only if an
unanticipated exception is encountered.

Use note() to emit an error message to standard error."""

__version__ = "$Revision: 2.8 $"

TestFailure = 'TestFailure'

def note(msg, *args):
    """Emit message to stderr, formatting message string with optional args."""
    import sys
    sys.stderr.write(msg % args + '\n')

def exercise(stmt, env, purpose, expected_exception=None, verbose=0):
    """Exec a statement in an environment, catching expected exceptions if any.

    Exec STATEMENT in ENV.

    PURPOSE describes the statements intended effect, for failure reports.

    Optional EXPECTED_EXCEPTION indicates exception that code is *supposed*
    to raise.

    If optional VERBOSE is set, note() exec of the statement."""
    
    import sys

    try:
        if verbose: note("Exercise: exec %s in env", `stmt`)
        exec stmt in env
        if expected_exception:
            raise TestFailure, ("Unanticipated success, %s (%s)"
                                % (`stmt`, purpose))
        return
    except:
        if sys.exc_type == expected_exception:
            return
        else:
            raise sys.exc_type, sys.exc_value, sys.exc_traceback

ModEnv = vars()
def test_exercise(verbose=0):
    """Exercise exercise, and demonstrate usage..."""
    env = ModEnv
    exercise('testee = 1',
             env, "Innocuous assignment", None, verbose)
    exercise('if testee != 1: raise SystemError, "env not working"',
             env, "Verify assignment", None, verbose)
    exercise('x(1) = 17',
             env, "Expecting basic syntax error", SyntaxError, verbose)
    exercise('{}[1]',
             env, "Expecting basic key error.", KeyError, verbose)

    env['env'] = env
    exercise("""exercise('1', env, 'Falsely expecting syntax error',
                         SyntaxError, %d)""" % verbose,
             env, "Capturing exercise exercise error", TestFailure, verbose)

    print 'test_exercise() passed.'

if __name__ == "__main__":

    test_exercise()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.