Alexander Steffen - Writing unit tests for C code in Python

preview_player
Показать описание
Alexander Steffen - Writing unit tests for C code in Python
[EuroPython 2016]
[21 July 2016]
[Bilbao, Euskadi, Spain]

There are many unit testing frameworks for C out there, but most of
them require you to write your tests in C (or C++). While there might
be good reasons to keep your implementation in C, those hardly apply
to the tests. So wouldn't it be nice to use all the power of Python
and its unit testing capabilities also for your C code? This talk will
show you how.

-----

There are many unit testing frameworks for C out there, but most of
them require you to write your tests in C (or C++). While there might
be good reasons to keep your implementation in C (for example
execution speed or resource consumption), those hardly apply to the
tests. So wouldn't it be nice to use all the power of Python and its
unit testing capabilities also for your C code?

This talk will show you how to combine CFFI and pycparser to easily
create Python unit tests for C code, without a single line of C
anywhere in the test cases. It will also cover creating mock functions
in Python, that can be used by the C code under test to hide external
dependencies. Finally, we will look at some of the challenges you
might face when trying to mix Python and C and what to do about them.
Рекомендации по теме
Комментарии
Автор

I like this idea and wanted to try it out when I realized that a lot of the code I want to test is actually implemented using or wrapped by macros.

unperrier
Автор

Great job Alexander. The idea of keeping the testing job simple by wring code in python is the main highlight, also in case there is crash of C code I don't see any point of running the rest of the program. However on the python side we can have Exception handelling of some sort of timeouts to handle the scenario. I wish to try it out myself, where can I find the the source code for this?

Thanks
Pratyush

pratyushraizada
Автор

This is a really cool idea... i think it'd get your unit tests done really quickly. I don't really do much C any more and most modern languages have testing built in. Some of the C programmers I know though, well their "religion" forbids the use of Python and I'd love too see their faces when you suggest they "pollute" their C codebase with something as "unclean" as Python. ;)

edgeeffect
Автор

Great idea, how about functions like malloc() which are also used by CPython?
Would it be possible to replace them? Well I guess I'll have to try it myself.

unperrier
Автор

But what about functions tha use pointers as arguments?

lectrolouve
Автор

What does the '-' (single hyphen) arg do at 12:15?

branbick
Автор

how can I do this with embedded code (cross-compiled) . Assuming I will compile for a not Windows or Linux system?

chris
Автор

This idea is fascinating to me
can I get reference to more information?

שראלפוירליכט
visit shbcf.ru