def test_to_check_exception_is_thrown(self):
# Arrange
c = Class()
# Act and Assert
self.assertRaises(NameError, c.do_something)
如果do_something
拋出測試通過一個例外。
但是我有一個屬性,當我用c.do_something
替換c.name = "Name"
時,我得到一個關於我的測試模塊未被導入的錯誤,並且Eclipse突出顯示了等號。
我如何測試一個屬性拋出異常?
編輯:
setattr
和getattr
是新的我。在這種情況下,他們肯定有幫助,謝謝。
由於python 2.7+應該簽出@RaphaelAhrens以下答案。這是更多pythonic http:// stackoverflow。com/a/23650764/601245 – 2016-09-09 14:55:24