The way we expose python to the client is by declaring a C# interface, and implementing it in python.
The layer that implements that interface is actually a facade, so there's very little logic there, and I got away without writing tests for it for a long time... However, I just needed to modify it, so decided to do the right thing :-)
Most of the testing I did until now used fake objects, so the first step was to get a mock library. I downloaded Michael Foord's Mock library which looks really nice and does everything I need at this point.
One thing I wanted to test is that my class implements the C# interface correctly in terms of methods' signatures. Otherwise IronPython throws an exception when it tries to map the arguments to the python method, or when it tries to convert the return value. However, my tests are in python, and calls to the object from python don't go through the type checking/conversion parts.
To overcome this, I call the methods using .NET reflection, which mimics a C# call more accurately and goes through the type checking code. Here's the utility class that helps streamline this process:
def __init__(self, obj):
self._obj = obj
self._cs_type = clr.GetClrType(type(obj))
mth = self._cs_type.GetMethod(name)
except TargetInvocationException, exc:
Now I can do something like:
py_server = MyServer()
server = CallThroughCSharpInterface(py_server)
And the call to foo is now checked for type problems.
The code above reuses a DynamicProxy class I already had. Here it is for completeness:
self.proxy = proxy
self.method_name = method_name
This only handles the basic cases - only method calls (no fields / properties) and the interface doesn't contain any overloaded methods.
That's all I needed, so I could keep it simple :-)