2013-10-08 82 views
0

有沒有人知道一個嘲弄或殘留的框架,它允許屬性甚至類屬性像這樣的裝飾器呢?蟒蛇屬性框架

class classproperty: 
    """ 
    Decorator to read-only static properties 
    """ 
    def __init__(self, getter): 
     self.getter = getter 
    def __get__(self, instance, owner): 
     return self.getter(owner) 

class Foo: 
    _name = "Name" 
    @classproperty 
    def foo(cls): 
     return cls._name 

我目前使用的是mockito,但是這不允許存儲屬性的殘留。

回答

2

使用unittest.mock.PropertyMock(可因爲Python 3.3):

from unittest import mock 
with mock.patch.object(Foo, 'foo', new_callable=mock.PropertyMock) as m: 
    m.return_value = 'nAME' 
    assert Foo.foo == 'nAME' 

注意:如果您使用Python版本低於3.3,使用mock