views:

71

answers:

4

If I'm writing a library in C that includes a Python interface, is it OK to just write unit tests for the functions, etc in the Python interface? Assuming the Python interface is complete, it should imply the C code works.

Mostly I'm being lazy in that the Python unit test thing takes almost zero effort to use.

thanks, -nick

+1  A: 

If you only care if the Python library works, then test that. This will give you significant confirmation that the C library is robust, but the maxim "if you didn't test it, it doesn't work" still mostly applies and I wouldn't export the library without the test harness.

You could, in theory, test that the processor microcode is doing its job properly but one usually doesn't.

msw
A: 

Ideally, you'd write unit tests for each.

Your Python library calls probably (hopefully?) don't have a one-to-one correspondence to your C library calls, because that wouldn't be a very Pythonic interface, so if you only unit test your Python interface, there would be variations and sequences of C library calls that weren't tested.

Josh Kelley
A: 

I see two mains restrictions to unit testing thru the Python interface. Whether it is OK to testing with those restrictions or not depends on what the library does, how it is implemented, and on the alignment of the Python interface on the interface of the C library.

  • the library can only be exercised the way the Python library is using it. This is not a problem as far as the Python interface is the only client of the C library.
  • the Python unit tests do not have access to the internals of the C library as unit tests written in C would have: only what is exposed via the Python interface is reachable. Therefore
    • if a problem arises after 10 calls to the Python interface, 10 calls will be needed to reproduce it, while a unit test written in C could create the fixture directly, without the 10 calls. This can make Python tests slower.
    • the Python unit tests couldn't be as isolated as C unit tests could be as they may not been able to reset the internals of the library
philippe
+5  A: 

Tests through the Python interface will be valuable acceptance tests for your library. They will not however be unit tests.

Unit tests are written by the same coders, in the same language, on the same platform as the unit which they test. These should be written too!

You're right, though, unit testing in Python is far easier than C++ (or even C, which is what you said!).

Johnsyweb