Blender build addon tests, no reload needed if changed addon-source, by unit-tests

Hallo,
If one has to test an addon, you want to build, unit-tests are great.

Wat do you want: You wright your assertions and test if they are OK or not, right?

If you find an error in you addon-source, you want to repair it and press “Run Script” again (you unittests namely) and without precausions it will not work, because the changed source is not read/loaded again. So the NOT WISHED remedy is: close Blender and open Blender again. AND THIS IS WHAT THIS thread is about!!!

In principle this is possible by using “import imp” and its imp.reload function!!
But in my Blender of yesterday compiled I get this strange behavior!

I did the following (all in the zip attachment!):

In the mainSource.py the class Rectangle is hidden as comment ==>
in the unittest Rectangle is used and gives (the expected) error message!

Removing in the source the ‘’’ (two times :wink: ) the test succeeds (as expected)

Again ‘’’ before and after’’’ running the test, the test succeeds and THIS IS TOTALLY unexpected???
What is the reason, how to change???

=======output of unittesting========
is in the zip …!!!

put the dir in addons dir and try youself, please

Attachments

miniUnit.zip (3.62 KB)

This works, you need to check if the modules are in locals() before reloading.


bl_info = {
    "name": "MiniExampleUnitTesting",
    "description": "setup a test such that changing source does not need a restart of Blender",
    "author": "PKHG",
    "version": (1, 0, 0),
    "blender": (2, 6, 3),
    "location": "View3D > Left panel ",
    "warning": '',  # used for warning icon and text in addons panel
    "wiki_url": "http://wiki.blender.org/",
    "tracker_url": "",
    "category": "Development"}

if "bpy" in locals():
    import imp
    if "mainSource" in locals():
        imp.reload(mainSource)
    if "testMainSource" in locals():
        imp.reload(testMainSource)
else:
    from . import mainSource, testMainSource

import bpy

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

 
if __name__ == "__main__":
    register()
    


No for unittesting this seems not to be the solution…

The mainSource.py objects available at some time stay available in the unittest.
A class changed is respected in the unittest though! AND this is what counts …
If a class is ‘deleted’ (or camouflated with ‘’’ ‘’’) one has to remove the test for it …

By the way, setUp class can be simplified without if else for this thing …

EDIT:
What works in my Blender version (build revision: 44917) is the following:
Changing the init.py file is discovered by running Blender and the ‘imp’ part is executed.
A reload without finishing Blender and starting again :wink: