Categories


Archives


Recent Posts


Categories


CMocka: Unit Testing C Code

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Coming at C from a “test legacy code first” point of view is tricky. All those neat test framework features for testing code not written under test (i.e. mocks, etc.) rely on a language’s dynamic nature for implementation details.

You don’t get that easily in C, so it was a pleasure to be introduced to the cmocka framework. In addition to being a fully featured unit test framework (pass/fail tests, fixtures, etc.), cmocka allows you to “mock” specific C functions in a particular test suite using the linker’s --wrap option.

I use the term mock loosely here. The system itself is better described as a monkey patching system (replace one function definition with another), but as userland C lacks the concept of an “object with methods attached”, monkey patching serves the same purpose as mock objects in other languages.

Copyright © Alan Storm 1975 – 2018 All Rights Reserved

Originally Posted: 22nd November 2017