I am trying to write unit test for the __init__ class:
def __init__(self, buildNum, configFile = "configfile.txt"): super(DevBuild, self).__init__(buildNum, configFile) if configFile == "configfile.txt": self.config.MakeDevBuild()
The config attribute is set super __init__ . I am using mock and I want the config attribute to be a mock object. However, I could not figure out how to do this. Here is the best I could come up with for the test:
def test_init(self): with patch('DevBuild.super', create=True) as mock_super: mock_MakeDevBuild = MagicMock() mock_super.return_value.config.MakeDevBuild = mock_MakeDevBuild
However, this does not work - I get an error message:
Error Traceback (most recent call last): File "/Users/khagler/Projects/BuildClass/BuildClass/test_devBuild.py", line 17, in test_init self.testBuild = DevBuild("42") File "/Users/khagler/Projects/BuildClass/BuildClass/DevBuild.py", line 39, in __init__ self.config.MakeDevBuild() AttributeError: 'DevBuild' object has no attribute 'config'
Clearly, I am not setting the config attribute correctly, but I have no idea where exactly I should set it. Or, if it is important, if what I want to do is even possible. Can someone tell me what I need to do to make this work?
khagler
source share