Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 418

Show
Ignore:
Timestamp:
07/06/05 19:10:21
Author:
fumanchu
Message:

Added optional code-coverage run to CP, including test suite. Install Rees or Batchelder's coverage.py to use it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r405 r418  
    4545_httpserver = None 
    4646 
     47codecoverage = False 
     48 
    4749try: 
    4850    from threading import local 
  • trunk/cherrypy/server.py

    r408 r418  
    3838import cherrypy 
    3939from cherrypy import _cphttptools 
    40 from cherrypy.lib import autoreload, profiler 
     40from cherrypy.lib import autoreload, profiler, covercp 
    4141 
    4242 
     
    6868    """ 
    6969     
     70    if cherrypy.codecoverage: 
     71        covercp.start() 
     72     
    7073    # Use a flag to indicate the state of the cherrypy application server. 
    7174    # 0 = Not started 
     
    190193    scheme: either "http" or "https"; defaults to "http" 
    191194    """ 
    192      
    193195    if cherrypy._appserver_state == 0: 
    194196        raise cherrypy.NotReady("No thread has called cherrypy.server.start().") 
     
    198200    threadID = threading._get_ident() 
    199201    if threadID not in seen_threads: 
     202         
     203        if cherrypy.codecoverage: 
     204            covercp.start() 
     205         
    200206        i = len(seen_threads) + 1 
    201207        seen_threads[threadID] = i 
  • trunk/cherrypy/test/test.py

    r408 r418  
    125125 
    126126 
     127def report_coverage(coverage): 
     128    localDir = os.path.dirname(__file__) 
     129    curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 
     130    basedir = os.path.normpath(os.path.join(curpath, '../')) 
     131     
     132    coverage.get_ready() 
     133    morfs = [x for x in coverage.cexecuted if x.startswith(basedir.lower())] 
     134     
     135    total_statements = 0 
     136    total_executed = 0 
     137     
     138    print 
     139    print "CODE COVERAGE (this might take a while)", 
     140    for morf in morfs: 
     141        sys.stdout.write(".") 
     142        sys.stdout.flush() 
     143        name = os.path.split(morf)[1] 
     144        try: 
     145            _, statements, _, missing, readable  = coverage.analysis2(morf) 
     146            n = len(statements) 
     147            m = n - len(missing) 
     148            total_statements = total_statements + n 
     149            total_executed = total_executed + m 
     150        except KeyboardInterrupt: 
     151            raise 
     152        except: 
     153            pass 
     154     
     155    pc = 100.0 
     156    if total_statements > 0: 
     157        pc = 100.0 * total_executed / total_statements 
     158     
     159    print ("\nTotal: %s Covered: %s Percent: %2d%%" 
     160           % (total_statements, total_executed, pc)) 
     161 
     162 
     163def run_test_suite(moduleNames, server, conf): 
     164    cherrypy.config.update({'global': conf.copy()}) 
     165    helper.startServer(server) 
     166    for testmod in moduleNames: 
     167        # Must run each module in a separate suite, 
     168        # because each module uses/overwrites cherrypy globals. 
     169        cherrypy.config.reset() 
     170        cherrypy.config.update({'global': conf.copy()}) 
     171        cherrypy._cputil._cpInitDefaultFilters() 
     172        suite = CPTestLoader.loadTestsFromName(testmod) 
     173        CPTestRunner(verbosity=2).run(suite) 
     174    helper.stopServer() 
     175 
     176 
    127177def main(): 
    128178    # Place our current directory's parent (cherrypy/) at the beginning 
     
    132182    sys.path.insert(0, os.path.normpath(os.path.join(curpath, '../../'))) 
    133183     
     184    # Start the coverage tool before importing cherrypy, so module-level 
     185    # global statements are covered. 
     186    try: 
     187        from coverage import the_coverage as coverage 
     188        coverage.cache_default = os.path.join(os.path.dirname(__file__), 
     189                                              "../lib/coverage.cache") 
     190        coverage.start() 
     191    except ImportError: 
     192        coverage = None 
     193     
     194    global cherrypy, helper 
    134195    print "Python version used to run this test script:", sys.version.split()[0] 
    135196    try: 
    136197        import cherrypy 
     198        from cherrypy.test import helper 
    137199    except ImportError: 
    138200        print "Error: couldn't find CherryPy !" 
     
    140202    print "CherryPy version", cherrypy.__version__ 
    141203    print 
    142      
    143     import cherrypy 
    144     from cherrypy.test import helper 
    145204     
    146205    class NotReadyTest(unittest.TestCase): 
     
    170229                   'server.logToScreen': False, 
    171230                   'server.environment': "production", 
    172                    'profiling.on': True, 
    173231                   } 
    174232     
    175     for name, server in [("Serverless", None), 
    176                          ("Native HTTP Server", "cherrypy._cphttpserver.embedded_server"), 
    177                          ("Native WSGI Server", "cherrypy._cpwsgi.WSGIServer"), 
    178                          ]: 
    179         print 
    180         print "Running tests:", name 
    181          
    182         cherrypy.config.update({'global': server_conf.copy()}) 
    183         helper.startServer(server) 
    184         for testmod in testList: 
    185             # Must run each module in a separate suite, 
    186             # because each module uses/overwrites cherrypy globals. 
    187             cherrypy.config.reset() 
    188             cherrypy.config.update({'global': server_conf.copy()}) 
    189             cherrypy._cputil._cpInitDefaultFilters() 
    190             suite = CPTestLoader.loadTestsFromName(testmod) 
    191             CPTestRunner(verbosity=2).run(suite) 
    192         helper.stopServer() 
    193      
     233    print 
     234    print "Running tests: Serverless" 
     235    cherrypy.codecoverage = True 
     236    run_test_suite(testList, None, server_conf) 
     237    cherrypy.codecoverage = False 
     238     
     239    print 
     240    print "Running tests: Native HTTP Server" 
     241    run_test_suite(testList, "cherrypy._cphttpserver.embedded_server", server_conf) 
     242     
     243    print 
     244    print "Running tests: Native WSGI Server" 
     245    server_conf['profiling.on'] = True 
     246    run_test_suite(testList, "cherrypy._cpwsgi.WSGIServer", server_conf) 
     247    del server_conf['profiling.on'] 
     248     
     249    if coverage: 
     250        report_coverage(coverage) 
     251        print "run /cherrypy/lib/covercp.py as a script to serve coverage results on port 8080" 
     252     
     253    print "run /cherrypy/lib/profiler.py as a script to serve profiling results on port 8080" 
     254     
     255    print 
    194256    raw_input('hit enter') 
    195257 

Hosted by WebFaction

Log in as guest/cpguest to create tickets