How to test a blender call when using blender as a module

We have a simple class that does this:


I want to test that we have called the proper functions with the proper names.
I do not seem to find a way to do that.

We are using purest.

with mocker.patch("bpy.ops.wm.save_as_mainfile") as mocked:

Gives me an error:

AttributeError: 'BPyOpsSubMod' object has no attribute 'save_as_mainfile'

My understanding that everything is a plugin and is initialised runtime.
I tried to mock the init function with no luck because you do not know on which object to mock it, and some of them have it private (shock)…

I tried to do maybe to get out the information from the console, but with no luck aswel.

(Pdb) bpy.ops.console.copy()
*** RuntimeError: Operator bpy.ops.console.copy.poll() failed, context is incorrect

What is the proper way to test that a call has been made to blender?

I’m guessing this is just not supported, and you’d have to add your own function that you can patch, that then calls the operator.

Operators are not registered as actual Python attributes on this module, they are looked up in the Blender registry of operators whenever a call is made.

Can’t we just patch the Blender registry and look at the patch there?

That registry is implemented in C and does not involve Python, it’s not practical for a Python testing framework to hook into that.

Okay, so what do you suggest?

We just wrap the operators in our code and test it against our code?

Yes, that seems the most practical solution.