I have a library that interfaces with an external tool and exposes some basic keywords to use from robotframework; This library is implemented as a python package, and I would like to implement extended functionality that implements complex logic, and exposes more keywords, within modules of this package. The package is given test case scope, but I’m not entirely sure how this works. If I suggest a few ways I have thought of, could someone with a bit more knowledge let me know where I’m on the right track, and where I’m barking up the wrong tree…
-
Use an instance variable – if the scope is such that the python interpreter will see the package as imported by the current test case (i.e this is treated as a separate package in different test cases rather than a separate instance of the same package), then on initialisation I could set a global variable
INSTANCEtoselfand then from another module within the package, importINSTANCEand use it. -
Use an instance dictionary – if the scope is such that all imports see the package as the same, I could use robot.running.context to set a dictionary key such that there is an item in the instance dictionary for each context where the package has been imported – this would then mean that I could use the same context variable as a lookup key in the modules that are based on this. (The disadvantage of this one is that it will prevent garbage collection until the package itself is out of scope, and relies on it being in scope persistently.)
-
A context variable that I am as of yet unaware of that will give me the instance that is in scope. The docs are fairly difficult to search, so it’s fully possible that there is something that I’m missing that will make this trivial. Also just as good would be something that allowed me to call the keywords that are in scope.
-
Some excellent possibility I haven’t considered….
So can anyone help?
Credit for this goes to Kevin O. from the robotframework user group, but essentially the magic lives in
robot.libraries.BuiltIn.BuiltIn().get_library_instance(library_name)which can be used like this: