2012-06-24 4 views

risposta

11

Ci sono molte scelte in PyDev a seconda di ciò che si vuole:

  1. destro clic su una cartella e selezionare l 'esecuzione come> Python unit-test' (verrà eseguito tutti i moduli di sotto della directory come unit test).

  2. Fare clic con il tasto destro su più moduli Python e scegliere 'Esegui come> Python unit-test' (caricherà i test per tutti quei moduli ed eseguirli).

  3. Creare un modulo che importi tutti i test con un nome diverso e selezionare "Esegui come> Python unit-test" per quel modulo.

cioè .:

from test_mod1 import Test as Test1 
from test_mod2 import Test as Test2 
+0

Il suggerimento n. 3 è esattamente ciò di cui ho bisogno, grazie. – Graeme

+2

Solo per notare, se diventa grande, potresti voler generare automaticamente quello con qualcosa come cog (http://nedbatchelder.com/code/cog/) –

0
import sys 
import unittest 
# add here some import paths 
sys.path.append(r'..\..') 


if True: #__name__ == '__main__': 
    try: 
     __file__ 
    except NameError: 
     path = '.' 
    else: 
     path = os.path.split(__file__)[0] 
    caseset = dict() 
    def addSuite(suite): 
     for case in suite: 
      if isinstance(case, unittest.TestSuite): 
       addSuite(case) 
       continue 
      key = case.id() 
      if key in caseset: 
##    print 'in:', type(case).__name__ 
       pass 
      else: 
##    print 'new:', type(case).__name__ 
       caseset[key] = case 

    for filename in os.listdir(path): # could also be os.walk 
     if filename.startswith('test_'): 
      filepath = os.path.join(path, filename) 
      modname, ext = os.path.splitext(filename) 
      if os.path.isfile(filepath) and ext.lower() in ('py', 'pyw'): 
       mod = __import__(modname) 
      else: 
       try: 
        mod = __import__(modname) 
       except ImportError: 
        ty, er, tb = sys.exc_info() 
        if tb.tb_next: 
         traceback.print_exception(ty, er, tb) 
        continue 

      cases = unittest.defaultTestLoader.loadTestsFromModule(mod) 
      addSuite(cases) 
    l = caseset.values() 
    suite = unittest.TestSuite(l) 
    ##if __name__ == '__main__': 
     ##unittest.TextTestRunner(verbosity=1).run(suite) # run all tests 

questo file importazioni tutti i moduli che iniziano con 'test_' nella stessa directory e li aggiunge alla variabile suite.

suite deve essere eseguito da pydev ma non riesco a testarlo - non ho pydev.

+0

in realtà non affrontare specificatamente il funzionamento in PyDev. – Graeme