Source

CherryPy / cherrypy / test / test.py

"""
Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

# Regression test suite for CherryPy

import sys
if len(sys.argv) == 2 and sys.argv[1] in ('-h', '--help'):
    print "Usage: unittest.py [testName+]"
    sys.exit(0)

python2={}
python2[3]={}    # Infos about python-2.3
python2[4]={}    # Infos about python-2.4

# Edit these lines to match your setup
if sys.platform=="win32":
    python2[3]['path']="c:\\python23\\python"
    python2[4]['path']="c:\\python24\\python"
else:
    python2[3]['path']="python2.3"
    python2[4]['path']="python2.4"

print "Checking that port 8000 is free...",
try:
    import socket
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('127.0.0.1', 8000))
    s.close()
    print "\n### Error: port 8000 is busy. This port must be free to run this test script"
    sys.exit(-1)
except socket.error:
    print "OK"

print

print "Examining your system..."
print
print "Python version used to run this test script:", sys.version.split()[0]
print
import buildInfoMap
python2 = buildInfoMap.buildInfoMap(python2)

print
print "Checking CherryPy version..."
import os
try:
    import cherrypy
except ImportError:
    print "Error: couln't find CherryPy !"
    os._exit(-1)

print "    Found version " + cherrypy.__version__
print

print "Testing CherryPy..."
failedList=[]
skippedList=[]

tutorialTestList = [
    ('01', ['/'], ['Hello world!']),
    ('02', ['/showMessage'], ['Hello world!']),
    ('03', ['/greetUser?name=Bob'], ["Hey Bob, what's up?"]),
    ('04', ['/links/extra/'], ['\n            <p>Here are some extra useful links:</p>\n\n            <ul>\n                <li><a href="http://del.icio.us">del.icio.us</a></li>\n                <li><a href="http://www.mornography.de">Hendrik\'s weblog</a></li>\n            </ul>\n\n            <p>[<a href="../">Return to links page</a>]</p>\n        ']),
    ('05', ['/another/'], ['\n            <html>\n            <head>\n                <title>Another Page</title>\n            <head>\n            <body>\n            <h2>Another Page</h2>\n        \n            <p>\n            And this is the amazing second page!\n            </p>\n        \n            </body>\n            </html>\n        ']),
    ('06', ['/'], ['\n            <html>\n            <head>\n                <title>Tutorial 6 -- Aspect Powered!</title>\n            <head>\n            <body>\n            <h2>Tutorial 6 -- Aspect Powered!</h2>\n        \n            <p>\n            Isn\'t this exciting? There\'s\n            <a href="./another/">another page</a>, too!\n            </p>\n        \n            </body>\n            </html>\n        ']), 
    ('07', ['/hendrik'], ['Hendrik Mans, CherryPy co-developer & crazy German (<a href="./">back</a>)']), 
    ('08', ['/', '/'], ["\n            During your current session, you've viewed this\n            page 1 times! Your life is a patio of fun!\n        ", "\n            During your current session, you've viewed this\n            page 2 times! Your life is a patio of fun!\n        "]), 
    ('09', ['/'], ['<html><body><h2>Generators rule!</h2><h3>List of users:</h3>Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/></body></html>']), 
]

testList = [
    'testObjectMapping',
    'testFilter1',
]

if len(sys.argv) > 1:
    # Some specific tests were specified on the command line
    # Limit the tests to these ones
    newTutorialTestList = []
    newTestList = []
    for number, urlList, resultList in tutorialTestList:
        if "tutorial%s" % number in sys.argv[1:]:
            newTutorialTestList.append([number, urlList, resultList])
    for t in testList:
        if t in sys.argv[1:]:
            newTestList.append(t)
    tutorialTestList = newTutorialTestList
    testList = newTestList

import helper

for version, infoMap in python2.items():
    print
    print "Running tests for python %s..."%infoMap['exactVersionShort']

    # Run tests based on tutorials
    for number, urlList, resultList in tutorialTestList:
        code = open('../tutorial/tutorial%s.py' % number, 'r').read()
        code = code.replace('tutorial.conf', 'testsite.cfg')
        print "    Testing tutorial %s..." % number,
        #if ((version == 1 and number in ('06', '09')) or
        #        (version == 2 and number in ('09'))):
        #    print "skipped"
        #    skippedList.append("Tutorial %s for python2.%s" % (number, version))
        #    continue
           
        helper.checkPageResult('Tutorial %s' % number, infoMap, code, "", urlList, resultList, failedList)

    # Running actual unittests
    for test in testList:
        exec("import "+test)
        eval(test+".test(infoMap, failedList, skippedList)")

print
print
print "#####################################"
print "#####################################"
print "###          TEST RESULT          ###"
print "#####################################"
print "#####################################"
if skippedList:
    print
    print "*** THE FOLLOWING TESTS WERE SKIPPED:"
    for skipped in skippedList: print skipped

    print "**** THE ABOVE TESTS WERE SKIPPED"
    print

if failedList:
    print
    print "*** THE FOLLOWING TESTS FAILED:"
    for failed in failedList: print failed

    print "**** THE ABOVE TESTS FAILED"
    print
    print "**** Some errors occured: please add a ticket in our Trac system (http://trac.cherrypy.org/cgi-bin/trac.cgi/newticket) with the output of this test script"

else:
    print
    print "**** NO TEST FAILED: EVERYTHING LOOKS OK ****"

############"
# Ideas for future tests:
#    - test if tabs and whitespaces are handled correctly in source file (option -W)
#    - test if absolute pathnames work fine on windows
#    - test sessions
#    - test threading server
#    - test forking server
#    - test process pooling server
#    - test SSL
#    - test compilator errors
#    - test abstract classes
#    - test hidden classes
#    ...
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.