Unittest - testing properties (read-only attributes)

Roy Smith roy at panix.com
Mon Feb 21 12:11:28 EST 2005


In article <uu0o5c0br.fsf at yahoo.co.uk>, Paul Moore <pf_moore at yahoo.co.uk> 
wrote:

> I have a class with a read-only attribute, and I want to add a unit
> test to ensure that it really *is* read-only. I can do this as
> 
>     def test_readonly(self):
> 	"""Value and multiplier must be readonly"""
> 	try:
> 	    self.combat.value = 1
> 	    self.fail("Value is not read only")
> 	except AttributeError:
> 	    pass
> 
> That works, but it seems a bit clumsy. Is there a better way?
> 
> Thanks,
> Paul.

You want something like

self.assertRaises(AttributeError, lambda: self.combat.value = 1)



More information about the Python-list mailing list