<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Unittesting CODESYS python scripts</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/d3817e5fcf/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/d3817e5fcf/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/d3817e5fcf/</id><updated>2020-02-27T14:48:30.139000Z</updated><subtitle>Recent posts to Unittesting CODESYS python scripts</subtitle><entry><title>Unittesting CODESYS python scripts</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/d3817e5fcf/?limit=25#ce53" rel="alternate"/><published>2020-02-27T14:48:30.139000Z</published><updated>2020-02-27T14:48:30.139000Z</updated><author><name>DavidCozens</name><uri>https://forge.codesys.com/u/davidcozens/</uri></author><id>https://forge.codesys.com84411da33bc93e91e3f3c8584ea491f0597aee8f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;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...&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Unittesting CODESYS python scripts</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/d3817e5fcf/?limit=25#567b" rel="alternate"/><published>2020-02-27T14:48:28.514000Z</published><updated>2020-02-27T14:48:28.514000Z</updated><author><name>DavidCozens</name><uri>https://forge.codesys.com/u/davidcozens/</uri></author><id>https://forge.codesys.com86d2f04dbc863fc8a1c391c580616ca108880ff6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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. &lt;/p&gt;
&lt;p&gt;As an example I have created a cutdown example using a couple of very simple files&lt;br/&gt;
&lt;strong&gt;Vendor.py&lt;/strong&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="n"&gt;Vendor&lt;/span&gt;(&lt;span class="n"&gt;object&lt;/span&gt;):
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;__init__&lt;/span&gt;(&lt;span class="k"&gt;self&lt;/span&gt;, &lt;span class="nb"&gt;name&lt;/span&gt;):
        &lt;span class="k"&gt;self&lt;/span&gt;.&lt;span class="nb"&gt;name&lt;/span&gt; = &lt;span class="nb"&gt;name&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;TestVendor.py&lt;/strong&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;mock&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;unittest&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Vendor&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestVendor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; 
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;testName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;vendor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vendor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Vendor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'VendorName'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'VendorName'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vendor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; 
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vm"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;#import sys;sys.argv = ['', 'Test.testName']&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This simple test runs and passes.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;@mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'librarymanager'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;testGetAllLibrariesCalled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="s1"&gt;'''&lt;/span&gt;
&lt;span class="s1"&gt;        '''&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;span class="w"&gt; &lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I get the following error when I try to run the test&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Zitat:&lt;/strong&gt;&lt;br/&gt;
TypeError: Need a valid target to patch. You supplied: 'librarymanager'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>