[英]Nosetest repeated init class during testing
我正在嘗試使用鼻子測試並具有以下結構
tests|-- test_main.py
|-- test_fileone.py
|-- test_filetwo.py
在test_main.py里面,我有以下代碼
class A(unittest.TestCase):
@classmethod
def setUpClass(self):
print "Hello World"
self.objIwant="12"
@classmethod
def tearDownClass(cls):
self.objIwant.quit()
在test_fileone.py內部
class B(A):
def test_loginpage(self):
testme(self.objIwant)
def test_logoutpage(self):
testme_other(self.objIWant)
#followed other def test_zzz(self)
在test_filetwo.py內部
class C(A):
def test_clickpage(self):
clickme(self.objIwant)
def test_revertpage(self):
revertme(self.objIWant)
#followed other def test_zzz(self)
我得到的結果是(按順序):
1. HelloWorld printed
2. Result of test_loginpage
3. Result of test_logoutpage
4. Helloworld printed
5. Result of test_clickpage
6. Result of test_revertpage
根據鼻子測試文檔,我知道鼻子測試將索引所有測試/文件夾和其中的文件,並對以test_zzz開頭的功能進行測試。 但是,這使我感到困惑,如何在類C和類B中實際使用objIWant,它們是從類A派生的,而沒有兩次打印“ Hello World”(初始化時只應打印一次,其他類應該可以訪問)來自父類的相同objIWant)
我如何才能做到這一點,從而更好地將我的單元測試模塊結構化?
從docs開始 ,為每個新的測試類調用setUpClass
:
當測試套件遇到來自新類的測試時,將調用前一個類(如果有)的tearDownClass(),然后調用新類的setUpClass()。
如果您希望所有測試類僅調用一次該方法,則可以使用該鏈接的下一段中所述的setUpModule
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.