2017-06-23 75 views
0

據我所知,setUpClass/tearDownClass方法被調用一次類,並且setUp()/tearDown()在每個單元測試之前/之後被調用。請參見下面的代碼片段於本標準設置:Python單元測試 - 使用setupclass和多個安裝/刪除?

class Example(unittest.TestCase): 
    @classmethod 
    def setUpClass(cls): 
     do_expensive_setup_of_immutable_objects_used_by_all_tests() 

    def setUp(self): 
     do_setup_before_each_test() 

    def test1(self): 
     do_test1_stuff() 

    def test2(self): 
     do_test1_stuff() 

    def tearDown(self): 
     do_teardown_after_each_test() 

    @classmethod 
    def tearDownClass(cls): 
     do_teardown_of_immutable_objects_used_from_all_tests() 

不過,我想一些單元測試,以從其他單元測試不同的setUp()tearDown()。什麼會被認爲是一個很好的測試結構來實現這個功能?我有什麼選擇?我對Python沒有經驗,希望知道一種很好的方式來編寫涉及昂貴的setUp()程序的單元測試。

+0

把它們在其他類。 –

+0

你可以更具體一點,就像把另一類放到什麼地方一樣? – bwrabbit

回答

1

也許這就是@Klaus的意思?

import unittest 


class TestA(unittest.TestCase): 
    def setUp(self): 
     print "Setup # 1" 

    def test1(self): 
     print "Doing test1" 

class TestB(unittest.TestCase): 
    def setUp(self): 
     print "Setup # 2" 

    def test2(self): 
     print "Doing test2" 


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

或者,您可以根據方法名稱更改設置。
雖然這可能是一個更難以維持的痛苦。

class TestA(unittest.TestCase): 
    def setUp(self): 
     if self._testMethodName == "test1": 
      print "Setup # 1" 
     elif self._testMethodName == "test2": 
      print "Setup # 2" 

    def test1(self): 
     print "Doing test1" 

    def test2(self): 
     print "Doing test2" 


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

都產生相同的輸出:

Output: 
Setup # 1 
Doing test1 
.Setup # 2 
Doing test2 
. 
---------------------------------------------------------------------- 
Ran 2 tests in 0.001s 

OK 

編輯: 對不起,我不認爲我完全理解,但這裏是我實現您的評論的嘗試。 我的方法是重新應用BaseTest.setUp(),而不是像你所描述的那樣儘量保持它完整。
TypeOneTestsTypeTwoTests繼承BaseTest他們使用super(...)

class BaseTest(unittest.TestCase): 
    def setUp(self): 
     print "BaseTest setUp" 

    def tearDown(self): 
     print "BaseTest tearDown" 

    # you could add tests here but I didn't add any based on your comment 


class TypeOneTests(BaseTest): 
    def setUp(self): 
     super(TypeOneTests, self).setUp() 
     print "TypeOneTests setUp" 

    def tearDown(self): 
     print "TypeOneTests tearDown" 

    def test(self): 
     print "a test in TypeOneTests" 


class TypeTwoTests(BaseTest): 
    def setUp(self): 
     super(TypeTwoTests, self).setUp() 
     print "TypeTwoTests additional setUp" 

    def tearDown(self): 
     print "TypeTwoTests tearDown" 
     super(TypeTwoTests, self).tearDown() 

    def test(self): 
     print "a test in TypeTwoTests" 


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

輸出調用BaseTest的裏面自己setUp功能:

BaseTest setUp 
TypeOneTests setUp 
a test in TypeOneTests 
TypeOneTests tearDown 
.BaseTest setUp 
TypeTwoTests additional setUp 
a test in TypeTwoTests 
TypeTwoTests tearDown 
BaseTest tearDown 
. 
---------------------------------------------------------------------- 
Ran 2 tests in 0.002s 

OK 
+0

感謝您的反饋。讓我再試一次解釋。我想要更類似於: BaseTest安裝| TypeOneTests設置(構建BaseTest設置)| 設置第一類單元測試| 運行單元測試| 拆除第一類單元測試| TypeOneTests拆解(完整保留BaseTest設置)| TypeTwoTests附加設置| 設置類型2單元測試| 運行單元測試| 第二類單元測試撕掉| BaseTest teardown | 我該如何做到這一點?再次感謝您的幫助。 – bwrabbit

+0

@bwrabbit我編輯了我的帖子,嘗試並實施您的評論。 – Robert

+0

非常感謝,您的編輯完成了我正在嘗試做的事情! :) – bwrabbit