I am new to test-driven development, but I’m loving it. There is, however, a main problem that prevents me from using it effectively.
I work for embedded medical applications, plain C, with safety issues.
Suppose you have module A
that has a function A_function()
that I want to test. This function call a function B_function
, implemented in module B
.
I want to decouple the module so, as James Grenning teaches, I create a Mock module B
that implements a mock version of B_function
.
However the day comes when I have to implement module B
with the real version of B_function
. Of course the two B_function
can not live in the same executable, so I don’t know how to have a unique “launcher” to test both modules.
James Grenning way out is to replace, in module A
, the call to B_function
with a function pointer that can have the value of the mock or the real function according to the need.
However I work in a team, and I can not justify this decision that would make no sense if it were not for the test, and no one asked me explicitly to use test-driven approach.
Maybe the only way out is to generate different a executable for each module.
Any smarter solution?
Thank you
5
Based upon what you’ve described, I would suggest that’s a horrible reason to use a function pointer. It will jack up your ability to debug, analyze core dumps, and it will complicate future development / maintenance. Function pointers have their place, but this isn’t one of them.
I think you’re approaching the testing from the wrong point of view. A_func depends upon B_func’s presence for it to operate. Therefore, you can’t truly test A without having the actual implementation of B in place. Putting a mock B_func in place will only generate a limited set of tests for A_func. By definition, you can’t fully exercise A_func without the real B_func in place.
Presuming you’re not dealing with a cascading series of module inclusions, the appropriate test suite would have some tests to exercise B_func and then some additional tests for exercising the variations that A_func can go through. It’s understood that you may have some redundancy between the tests for B_func and the indirect testing of B_func from A_func, but it’s generally not a big deal.
4