我有一個unittest
測試文件包含四個測試類,每個測試類負責在一個特定的類上運行測試。每個測試課程使我們完全相同set-up
和teardown
方法。 set-up
方法相對較大,啓動約20個不同的變量,而teardown
方法只是將這20個變量重置爲其初始狀態。python單元測試家務
到目前爲止,我一直把這20個變量放在四個setUp類的每一箇中。這有效,但不容易維護;如果我決定改變一個變量,我必須在所有四個setUp方法中改變它。然而,我尋找更優雅的解決方案失敗了。理想情況下,我只想輸入一次我的二十個變量,然後在四種方法中分別調用它們,然後在每種測試方法之後將它們拆下。考慮到這一點,我嘗試將變量放在一個單獨的模塊中,並在每個setUp中導入這些變量,但當然這些變量只能在設置方法中使用(另外,儘管我無法確切地說明原因,這種感覺就像做
from unittest import TestCase
class Test_Books(TestCase):
def setup():
# a quick and easy way of making my variables available at the class level
# without typing them all in
def test_method_1(self):
# setup variables available here in their original state
# ... mess about with the variables ...
# reset variables to original state
def test_method_2(self):
# setup variables available here in their original state
# etc...
def teardown(self):
# reset variables to original state without having to type them all in
class Books():
def method_1(self):
pass
def method_2(self):
pass
顯示實際的代碼,而不是說明你正在做什麼的評論,而不是顯示你在做什麼時,確切的代碼是問題。 – geoffspear