开发者

trickle down unit tests

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 tes开发者_如何学Ct thing takes almost zero effort to use.

thanks, -nick


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!).


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜