Type StubOutForTesting
Namespace tensorflow.test
Parent PythonObjectContainer
Interfaces IStubOutForTesting, IContextManager<T>
Support class for stubbing methods out for unit testing. Sample Usage: You want os.path.exists() to always return true during testing. stubs = StubOutForTesting()
stubs.Set(os.path, 'exists', lambda x: 1)
...
stubs.CleanUp() The above changes os.path.exists into a lambda that returns 1. Once
the... part of the code finishes, the CleanUp() looks up the old
value of os.path.exists and restores it.
Methods
- CleanUp
- CleanUp_dyn
- Set
- Set_dyn
- SmartSet
- SmartSet_dyn
- SmartUnsetAll
- SmartUnsetAll_dyn
- UnsetAll
- UnsetAll_dyn
Properties
Public instance methods
void CleanUp()
Undoes all SmartSet() & Set() calls, restoring original definitions.
object CleanUp_dyn()
Undoes all SmartSet() & Set() calls, restoring original definitions.
void Set(object parent, object child_name, object new_child)
In parent, replace child_name's old definition with new_child. The parent could be a module when the child is a function at
module scope. Or the parent could be a class when a class' method
is being replaced. The named child is set to new_child, while the
prior definition is saved away for later, when UnsetAll() is
called. This method supports the case where child_name is a staticmethod or a
classmethod of parent.
Parameters
-
object
parent - The context in which the attribute child_name is to be changed.
-
object
child_name - The name of the attribute to change.
-
object
new_child - The new value of the attribute.
object Set_dyn(object parent, object child_name, object new_child)
In parent, replace child_name's old definition with new_child. The parent could be a module when the child is a function at
module scope. Or the parent could be a class when a class' method
is being replaced. The named child is set to new_child, while the
prior definition is saved away for later, when UnsetAll() is
called. This method supports the case where child_name is a staticmethod or a
classmethod of parent.
Parameters
-
object
parent - The context in which the attribute child_name is to be changed.
-
object
child_name - The name of the attribute to change.
-
object
new_child - The new value of the attribute.
void SmartSet(object obj, object attr_name, object new_attr)
Replace obj.attr_name with new_attr. This method is smart and works at the module, class, and instance level
while preserving proper inheritance. It will not stub out C types however
unless that has been explicitly allowed by the type. This method supports the case where attr_name is a staticmethod or a
classmethod of obj. Notes:
- If obj is an instance, then it is its class that will actually be
stubbed. Note that the method Set() does not do that: if obj is
an instance, it (and not its class) will be stubbed.
- The stubbing is using the builtin getattr and setattr. So, the __get__
and __set__ will be called when stubbing (TODO: A better idea would
probably be to manipulate obj.__dict__ instead of getattr() and
setattr()).
Parameters
-
object
obj - The object whose attributes we want to modify.
-
object
attr_name - The name of the attribute to modify.
-
object
new_attr - The new value for the attribute.
object SmartSet_dyn(object obj, object attr_name, object new_attr)
Replace obj.attr_name with new_attr. This method is smart and works at the module, class, and instance level
while preserving proper inheritance. It will not stub out C types however
unless that has been explicitly allowed by the type. This method supports the case where attr_name is a staticmethod or a
classmethod of obj. Notes:
- If obj is an instance, then it is its class that will actually be
stubbed. Note that the method Set() does not do that: if obj is
an instance, it (and not its class) will be stubbed.
- The stubbing is using the builtin getattr and setattr. So, the __get__
and __set__ will be called when stubbing (TODO: A better idea would
probably be to manipulate obj.__dict__ instead of getattr() and
setattr()).
Parameters
-
object
obj - The object whose attributes we want to modify.
-
object
attr_name - The name of the attribute to modify.
-
object
new_attr - The new value for the attribute.
void SmartUnsetAll()
Reverses SmartSet() calls, restoring things to original definitions. This method is automatically called when the StubOutForTesting()
object is deleted; there is no need to call it explicitly. It is okay to call SmartUnsetAll() repeatedly, as later calls have
no effect if no SmartSet() calls have been made.
object SmartUnsetAll_dyn()
Reverses SmartSet() calls, restoring things to original definitions. This method is automatically called when the StubOutForTesting()
object is deleted; there is no need to call it explicitly. It is okay to call SmartUnsetAll() repeatedly, as later calls have
no effect if no SmartSet() calls have been made.
void UnsetAll()
Reverses Set() calls, restoring things to their original definitions. This method is automatically called when the StubOutForTesting()
object is deleted; there is no need to call it explicitly. It is okay to call UnsetAll() repeatedly, as later calls have no
effect if no Set() calls have been made.
object UnsetAll_dyn()
Reverses Set() calls, restoring things to their original definitions. This method is automatically called when the StubOutForTesting()
object is deleted; there is no need to call it explicitly. It is okay to call UnsetAll() repeatedly, as later calls have no
effect if no Set() calls have been made.