2010-04-09 207 views
1

我有一個測試套件來執行煙霧測試。我將所有腳本存儲在各個類中,但是當我嘗試運行測試套件時,如果它在某個類中,我似乎無法使其工作。該代碼是如下:(a類調用測試)問題的運行單元測試測試套件OO

from alltests import SmokeTests 

class CallTests(SmokeTests): 

    def integration(self): 

     self.suite() 

if __name__ == '__main__': 
    run = CallTests() 
    run.integration() 

並測試套件:

class SmokeTests(): 

    def suite(self): #Function stores all the modules to be tested 
     modules_to_test = ('external_sanity', 'internal_sanity') 
     alltests = unittest.TestSuite() 
     for module in map(__import__, modules_to_test): 
      alltests.addTest(unittest.findTestCases(module)) 
     return alltests 
if __name__ == '__main__': 
    unittest.main(defaultTest='suite') 

該輸出的一個錯誤: 屬性錯誤:「模塊」對象沒有屬性「套件'

所以我可以看到如何調用一個正常的函數定義,但我發現很難調用套件。在浴室的設置,像這樣一個測試:

class InternalSanityTestSuite(unittest.TestSuite): 

# Tests to be tested by test suite 
def makeInternalSanityTestSuite(): 
    suite = unittest.TestSuite() 
    suite.addTest(TestInternalSanity("BasicInternalSanity")) 
    suite.addTest(TestInternalSanity("VerifyInternalSanityTestFail")) 
    return suite 

def suite(): 
    return unittest.makeSuite(TestInternalSanity) 

如果我有someSuite()類SmokeTests蟒蛇裏面找不到屬性套裝,但如果我刪除了一流的IT工作的。我將它作爲腳本運行,並將變量調用到測試中。我不想通過os.system('python tests.py')運行測試。我希望通過班級的測試來調用我像其他功能一樣

任何人都可以幫助我實現這個目標嗎?

感謝您提前提供任何幫助。

+1

-1:請不要完全重寫您發佈的代碼。關閉該問題並用全新的代碼發佈新問題。 – 2010-04-09 14:24:37

+0

@everyone - 在下一個問題中得到這個工作。 – chrissygormley 2010-04-12 11:22:48

回答

3

我知道這不是答案,但我建議使用可以使用測試發現的庫,如Python 2.7+的nose或unittest功能。

可能性做

nosetests module.submodule 

nosetests module.submodule:TestCase.test_method 

是無價的:)

1

這可不行:

class SmokeTests(): 

    def suite(self): #Function stores all the modules to be tested 
     modules_to_test = ('external_sanity', 'internal_sanity') 
     alltests = unittest.TestSuite() 
     for module in map(__import__, modules_to_test): 
      alltests.addTest(unittest.findTestCases(module)) 
     return alltests 

if __name__ == '__main__': 
    unittest.main(defaultTest='suite') 

此輸出的一個錯誤:屬性錯誤:'模塊'對象h因爲沒有屬性'套件'。

您的套房的方法值爲SmokeTests().suite()。請注意一個名爲suite的變量,因爲您沒有這樣的變量。

爲您的套件使用簡單功能更容易。

def someSuite(): 
    modules_to_test 
    ... 
    return alltests 

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

這樣的事情會更接近正確的。