3 ms·
This is obviously not an option for embedded stuff, but on many common platforms, it's actually easiest to unit-test C from Python. While you do need to write e
by int_19h 10y ago
This is obviously not an option for embedded stuff, but on many common platforms, it's actually easiest to unit-test C from Python. While you do need to write extra glue code (although ctypes are often "good enough" for tests, significantly reducing that amount), it's just so much easier to set things up in Python - i.e. generate inputs (especially strings!), set up files etc.
- pagnol 10y agoWould you mind giving an example of how you would unit test a function?
- int_19h 10y agoThe same way you would unit test a Python function, except you invoke the actual C function using e.g. ctypes: https://docs.python.org/3/library/ctypes.html https://docs.python.org/3/library/ctypes.html but your unit test is written in Python, including all the setup/teardown stuff and asserts. And you run it using the stock Python test runner (or some wrapper around that): https://docs.python.org/3/library/unittest.html https://docs.python.org/3/library/unittest.html (In theory, this all can be done even smoother if you use Cython, because that lets you interface with C more naturally than ctypes. I haven't tried that, though.) Note that this all makes sense for C APIs, not C++ ones. With C++ APIs, as soon as classes etc come into the picture, and especially once you start dealing with STL and Boost data types, it becomes too difficult to invoke that stuff from Python. However, the implementation can be in any language - what matters is what the tested API looks like.
- codebeaker 10y agoI'd be really interested to learn more about your technique too, sounds intriguing.