2011-11-02 25 views
5

tôi đã cố gắng:Làm thế nào để tôi tạo một thử nghiệm Suite trong python unittest

def buildTestSuite(): 
    suite = unittest.TestSuite() 
    for testcase in glob.glob('src/testsuite/test_*.py'): 
     module = os.path.splitext(testcase)[0] 
     print module 
     print type(module) 
     suite.addTest(__import__(module).buildTestSuite()) 
    return suite 

nhưng tôi nhận được eror:

Traceback (most recent call last): 
    File "runtests.py", line 63, in ? 
    results = main() 
    File "runtests.py", line 57, in main 
    results = unittest.TextTestRunner().run(buildTestSuite()) 
    File "runtests.py", line 53, in buildTestSuite 
    suite.addTest(__import__(module).buildTestSuite()) 
AttributeError: 'module' object has no attribute 'buildTestSuite' 

Trả lời

5
def buildTestSuite(): 
    suite = unittest.TestSuite() 
    for testcase in glob.glob('src/testsuite/test_*.py'): 
     modname = os.path.splitext(testcase)[0] 
     module=__import__(modname,{},{},['1']) 
     suite.addTest(unittest.TestLoader().loadTestsFromModule(module)) 
    return suite 
+0

lỗi: Traceback (cuộc gọi gần đây nhất): File "runtes ts.py ", dòng 64, trong? results = main() Tệp "runtests.py", dòng 58, trong chính results = unittest.TextTestRunner(). Run (buildTestSuite()) Tệp "runtests.py", dòng 52, trong buildTestSuite module = __import __ (modname, fromlist = '1') LoạiError: __import __() không có đối số từ khóa – kamal

+0

'__import__' chấp nhận đối số từ khóa trong Python2.6 + (ít nhất). Bạn đang sử dụng phiên bản Python nào? – unutbu

+0

Tôi đang sử dụng Python 2.4.3 – kamal

0

Hãy thử một cái gì đó như:

suite = unittest.TestSuite() 
for t in glob.glob('src/testsuite/test_*.py'): 
    try: 
     # If the module defines a suite() function, call it to get the suite. 
     mod = __import__(t, globals(), locals(), ['suite']) 
     suitefn = getattr(mod, 'suite') 
     suite.addTest(suitefn()) 
    except (ImportError, AttributeError): 
     # else, just load all the test cases from the module. 
     suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t)) 
+1

tôi gặp lỗi: Traceback (cuộc gọi gần đây nhất): Tệp "runtests.py", dòng 76, trong? results = main() Tệp "runtests.py", dòng 70, trong chính results = unittest.TextTestRunner(). Run (buildTestSuite()) Tệp "/usr/lib64/python2.4/unittest.py" , dòng 696, chạy thử nghiệm (kết quả) LoạiError: đối tượng 'NoneType' không thể gọi – kamal

Các vấn đề liên quan