I'd like to test function:
#foo_module.py
def foo(*args, **kwargs):
bar = SomeClass(*args, **kwargs)
# actions with bar
del bar
I chose to test the mock library. My test looks like:
@mock.patch('path.to.foo_module.SomeClass')
def test_foo(self, mock_class):
foo()
mock_class.assert_called_once_with()
But how I can check that 'del bar' executed? Call of mock_class.return_value.__del__
raises AttributeError.
UPD : Sorry but I didn't mention that SomeClass is django.contrib.gis.gdal.datasource.DataSource
. DataSource has overridden __del__
method:
def __del__(self):
"Destroys this DataStructure object."
if self._ptr and capi:
capi.destroy_ds(self._ptr)
In this case del bar
have effect outside the function. So I simple should to mock capi
and check capi.destroy_ds.called
.
Check the dir() result to see that object is still there:
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> a=10
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a']
>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
For this script:
def f() :
a = 10
print dir()
del a
print dir()
objLst = dir()
if 'a' not in objLst :
print 'Object deleted'
f()
you get the output:
['a']
[]
Object deleted
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.