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 files Vendor.py
classVendor(object):
  def__init__(self, name):
    self.name = name
TestVendor.py
importmockimportunittestimportVendorclassTestVendor(unittest.TestCase):Â Â Â Â deftestName(self):Â Â Â Â vendor=Vendor.Vendor('VendorName')Â Â Â Â self.assertEqual('VendorName',vendor.name)Â Â Â Â if__name__=="__main__":Â Â #import sys;sys.argv = ['', 'Test.testName']Â Â unittest.main()
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 , 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.
  @mock.patch('librarymanager')  deftestGetAllLibrariesCalled(self):    '''    '''Â
I get the following error when I try to run the test
Zitat:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've managed to solve this myself. Rather than battering my head trying to work out how to mock a global like librarymanager, I have created a class to wrap the codesys interface, and used dependency injection to pass this to my script. I can then create a mock using mock.create_autospec(CodesysInterface). A much nicer solution...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 files
Vendor.py
TestVendor.py
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 , 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
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.
I've managed to solve this myself. Rather than battering my head trying to work out how to mock a global like librarymanager, I have created a class to wrap the codesys interface, and used dependency injection to pass this to my script. I can then create a mock using mock.create_autospec(CodesysInterface). A much nicer solution...