CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Thu Oct 19, 2017 7:12 pm

All times are UTC+01:00

Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Oct 13, 2016 4:34 pm 

Joined: Fri Nov 23, 2012 11:42 am
Posts: 46
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

class Vendor(object):

    def __init__(self, name): = name

import mock
import unittest
import Vendor

class TestVendor(unittest.TestCase):
    def testName(self):
        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.

    def testGetAllLibrariesCalled(self):

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.

PostPosted: Fri Oct 14, 2016 12:09 pm 

Joined: Fri Nov 23, 2012 11:42 am
Posts: 46
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...

Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited