2015-11-07 63 views
5

nose發現過程查找名稱以test開頭的所有模塊,並在其中包含名稱中包含test的所有函數,並嘗試將它們作爲單元測試運行。見http://nose.readthedocs.org/en/latest/man.html使用名稱'test'忽略功能

我有一個函數,其名稱是說,make_test_account,在文件accounts.py。我想在名爲test_account的測試模塊中測試該功能。因此,在該文件的開頭我做的:

from foo.accounts import make_test_account 

但現在我發現鼻子對待功能make_test_account作爲一個單元測試,並嘗試運行它(因爲它沒有任何參數傳遞其失敗,這是必要的)。

如何確保鼻子專門忽略該功能?我寧願這樣做,這意味着我可以調用鼻子作爲nosetests,沒有任何命令行參數。

回答

4

鼻子有一個nottest裝飾。但是,如果您不想在要導入的模塊中應用@nottest裝飾器,也可以在導入後簡單地修改該方法。保持單元測試邏輯接近單元測試本身可能更清潔。

from foo.accounts import make_test_account 
# prevent nose test from running this imported method 
make_test_account.__test__ = False 

,您仍然可以使用nottest但它有同樣的效果:

from nose.tools import nottest 
from foo.accounts import make_test_account 
# prevent nose test from running this imported method 
make_test_account = nottest(make_test_account) 
+0

感謝這個額外的信息! – jwg

+1

這看起來比dm295接受的答案要好很多,因爲它並沒有在生產代碼中加入測試特定的(更重要的是 - **測試框架特定的**)代碼。感謝這個答案! – dsoosh

+0

這看起來更好,因爲它可以在不引入鼻子依賴的情況下跨越測試跑步者。 – weberc2

5

告訴鼻子,該功能不是測試 - 使用nottest修飾符。

# module foo.accounts 

from nose.tools import nottest 

@nottest 
def make_test_account(): 
    ...