2011-02-11 4 views
-1

다음 python 스크립트는 현재 폴더의 모든 pyunit 테스트를 검색하고 실행합니다. 첨부 된 pyunit python 스크립트에서 하위 폴더의 테스트를 검색하는 방법

 

"""Regression testing framework 

This module will search for scripts in the same directory named 
XYZtest.py. Each such script should be a test suite that tests a 
module through PyUnit. (As of Python 2.1, PyUnit is included in 
the standard library as 'unittest'.) This script will aggregate all 
found test suites into one big test suite and run them all at once. 
""" 

import sys, os, re, unittest 

def regressionTest(): 
    path = os.path.abspath(os.path.dirname(sys.argv[0])) 
    files = os.listdir(path) 
    print 'You are HERE! ' ,os.getcwd() 
    test = re.compile(".test\.py$", re.IGNORECASE) 
    files = filter(test.search, files) 
    filenameToModuleName = lambda f: os.path.splitext(f)[0] 
    moduleNames = map(filenameToModuleName, files) 
    modules = map(__import__, moduleNames) 
    load = unittest.defaultTestLoader.loadTestsFromModule 
    return unittest.TestSuite(map(load, modules)) 

if __name__ == "__main__": 
    unittest.main(defaultTest="regressionTest") 

어떻게 하위 폴더에서 테스트 파일이 지롱 검색을 얻을 수 있습니다.

아마 같은 :

 
import sys, os, re, unittest, fnmatch 
 

    for root, dirs, files in os.walk(path): 
     for filename in fnmatch.filter(files, pattern): 
      print os.path.join(root, filename) 
+0

[nose] (http://somethingaboutorange.com/mrl/projects/nose/1.0.0/)를 사용할 수 있습니다. 이것은 거의 모든 것을 자동으로 수행합니다. – chmullig

답변

0

변경 :

def regressionTest(): 
    path = os.path.abspath(os.path.dirname(sys.argv[0])) 

사람 : 다음

def regressionTest(somearg): 
    path = os.path.abspath(os.path.dirname(somearg)) 

그리고 :

,536,
for root, dirs, files in os.walk(path): 
    regressionTest(root) 
관련 문제