在Python中使用unittest
模塊時,如何臨時禁用單個單元測試?暫時禁用單個Python單元測試
51
A
回答
95
使用裝飾器unittest.skip
可以禁用單獨的測試方法或類。
@unittest.skip("reason for skipping")
def test_foo():
print('This is foo test case.')
@unittest.skip # no reason needed
def test_bar():
print('This is bar test case.')
有關其他選項,請參閱文檔Skipping tests and expected failures。
2
9
The latest version (2.7 - unreleased) supports test skipping/disabling like so。你可以獲得這個模塊並在現有的Python安裝中使用它。它可能會工作。
在此之前,我曾經將我想要的測試重新命名爲xtest_testname
,從test_testname
。
這是一個快速的elisp腳本來做到這一點。我的elisp有點生疏,所以我提前爲任何問題道歉。未經測試。
(defun disable_enable_test()
(interactive "")
(save-excursion
(beginning-of-line)
(search-forward "def")
(forward-char)
(if (looking-at "disable_")
(zap-to-char 1 ?_)
(insert "disable_"))))
22
你可以用裝飾來禁用它可以包含的功能和防止googletest或Python單元測試運行測試用例的測試。
def disabled(f):
def _decorator():
print f.__name__ + ' has been disabled'
return _decorator
@disabled
def testFoo():
'''Foo test case'''
print 'this is foo test case'
testFoo()
輸出:
testFoo has been disabled
4
我剛重命名測試用例方法以下劃線:test_myfunc變得_test_myfunc。
5
相關問題
- 1. 禁用單元測試MSTest
- 2. 運行單元測試時禁用bootstrap.js
- 3. 瓶禁用CSRF在單元測試
- 4. Python類和單元測試
- 5. Python中的單元測試
- 6. Python的單元測試ModuleImportFailure
- 7. Python單元測試細節
- 8. python單元測試方法
- 9. 氣流Python單元測試?
- 10. 單元測試在Python
- 11. 單元測試兩個Python 2和3
- 12. 在單元測試期間禁用python日誌記錄
- 13. 單擊時暫時禁用鏈接?
- 14. 單元測試DAL - 停止運行單元測試時連接
- 15. 時間戳測試單元測試
- 16. 用Python單元測試嘲弄side_effect
- 17. Python的單元測試調用混亂
- 18. 用Python編寫單元測試
- 19. 單元測試超時
- 20. 單元測試超時
- 21. 單元測試()
- 22. 單元測試
- 23. 單元測試
- 24. 單元測試
- 25. 單元測試
- 26. 單元測試
- 27. 單元測試
- 28. 單元測試
- 29. 單元測試
- 30. 單元測試
在Python 3中,Akif的答案在'@ unittest.SkipTest'下面起作用,而不是'@ unittest.skip' – lifebalance 2017-05-13 18:48:22
我使用Python 3.6.1和'@ unittest.skip'也很好。 – Pete 2017-06-13 13:15:01
@Pete,在Python 3.4.0中,'@ unittest.skip'不起作用。 – lifebalance 2017-07-09 06:17:26