How should I test the python shell generated by SWIG

I need to create a python shell for a library using SWIG and write unit tests for it. I do not know how to do that. My first take of this problem is to simulate a dynamic library with the same interface as those libraries for which I am writing a wrapper. This mock library can log every call or return some generated data. These logs and generated data can be checked by unit tests.

+4
source share
1 answer

I have many situations when I use a SWIG-generated shell for unit testing the library, but I assume that you request this unit testing the shell itself.

As I tested the shell, let's say mylib:

Properties . For each class, say MyClasswith properties open, I use MyClass._swig_getmethods__to list all properties that can be changed. I verify that the correct number of properties are available and they work as expected

# Filter out builtin_function_type
getmethods = {k: v for k,v in mylib.MyClass.__swig_getmethods__.iteritems() if type(v) != types.BuiltinFunctionType}
# Filter out lambda functions (constructors)
getmethods = {k: v for k,v in getmethods.iteritems() if v.func_name != '<lambda>'}.keys()

nGetSuccess = 0
testMe = set()
m = mylib.MyClass()
for method in getmethods:
    try:
      value = eval('m.'+method)
      nGetSuccess = nGetSuccess + 1
    except Exception as e:
      print(e.message)
self.assertEqual(nGetSuccess,len(getmethods))

Static methods

mylib.__dict__ . , , C struct Python , .

0

All Articles