Please forgive my if this is obvious, I'm not a python expert. I have some scripts that work inside CODESYS, but I need to extend them and I want them to be maintainable, so I want to introduce a Unittest framework around the modules before refactoring and then adding more code.
I want my unit tests to be fast so that I can use TDD, so I don't want to work inside CODESYS. Instead I want to work using activepython and to be able to use the features of mock and mock.patch to replace the CODESYS API calls. But I haven't yet managed to get this to work.
As an example I have created a cutdown example using a couple of very simple filesVendor.py
def __init__(self, name):
self.name = name
vendor = Vendor.Vendor('VendorName')
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
This simple test runs and passes.
I want to add some functionality that uses the librarymanager, the first test that I want is to verify that there is a single call to librarymanager.get_all_libraries()
, when I create a Vendor object. One of my many attempts starts like this, I've just tried to declare the module I am mocking, no actual test.
I get the following error when I try to run the test
TypeError: Need a valid target to patch. You supplied: 'librarymanager'
How would I write this first test? i.e. mock the module, mock the function, and assert that the function is called. I'm sure if I can get this first call tested the rest will be straightforward.
If I'm going down the wrong track, and I would be better trying this unit testing using different tools then please let me know.