unit testing properties

Mark McEahern marklists at mceahern.com
Fri Jan 18 09:53:36 EST 2002


Duncan Booth wrote:
> How about using setattr?
> def testSetter(self):
>     f = Foo()
>     self.assertRaises(AttributeError, setattr, f, 'bar', 42)

Excellent, that works!  Thanks!  I always forget about setattr.  Here's full
sample code:

    #! /usr/bin/env python

    import unittest

    class foo(object):

        def getBar(self):
            return 0

        bar = property(getBar, None, None, "bar.")

    class testFoo(unittest.TestCase):

        def testSetter(self):
            f = foo()
            self.assertRaises(AttributeError, setattr, f, 'bar', 42)

    if __name__=="__main__":
        unittest.main()

// mark





More information about the Python-list mailing list