<p>I've just started writing unit tests for a legacy code module with large physical dependencies using the #include directive. I've been dealing with them a few ways that felt overly tedious (providing empty headers to break long #include dependency lists, and using #define to prevent classes from being compiled) and was looking for some better  strategies for handling these problems.</p>

<p>I've been frequently running into the problem of duplicating almost every header file with a blank version in order to separate the class I'm testing in it's entirety, and then writing substantial stub/mock/fake code for objects that will need to be replaced since they're now undefined.</p>

<p>Anyone know some better practices?</p>

[{'answer_id': 65121, 'author': 'Pieter', 'author_id': 5822, 'author_profile': 'https://Stackoverflow.com/users/5822', 'pm_score': 1, 'selected': False, 'text': '<p>Since you\'re testing legacy code I\'m assuming you can\'t refactor said code to have less dependencies (e.g. by using the <a href="http://www.gotw.ca/gotw/028.htm" rel="nofollow noreferrer">pimpl idiom</a>)</p>\n\n<p>That leaves you with little options I\'m afraid. Every header that was included for a type or function will need a mock object for that type or function for everything to compile, there\'s little you can do...</p>\n'}, {'answer_id': 65166, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you keep writing stubs/mock/fake codes you risk doing unit testing on a class that has different behavior then when compiled on the main project.</p>\n\n<p>But if those includes are there and have no added behavior then it's Ok.</p>\n\n<p>I'd try not changing anything on the includes while doing the unit testing so you're sure (as far you can be on legacy code :) ) that you testing the real code.</p>\n"}, {'answer_id': 65168, 'author': 'Jeroen Dirks', 'author_id': 7743, 'author_profile': 'https://Stackoverflow.com/users/7743', 'pm_score': 1, 'selected': False, 'text': '<p>I am not answering your question directly but I am afraid that unit testing just may not be the thing to do if you work with large amounts of legacy code.</p>\n\n<p>After leading an XP team on a green field development project I really loved my Unit tests. Things happened and a few years later I find myself working on a large legacy code base that has lots of quality problems.</p>\n\n<p>I tried to find a way to add units tests to the application but in the end just got stuck in a catch-22:</p>\n\n<ol>\n<li>In order to write meaning full unit tests the code would need to be refactored.</li>\n<li>Without unit tests it will be too dangerous to refactor the code.</li>\n</ol>\n\n<p>If you feel like a hero and drink the cool-aid on unit testing then you may still give it a try but there is a real risk that you end up with just more test code of little value that now also needs to be maintained.</p>\n\n<p>Sometimes it is just best to work on the code in the way that is "designed" to be worked on.</p>\n'}, {'answer_id': 65258, 'author': 'quamrana', 'author_id': 4834, 'author_profile': 'https://Stackoverflow.com/users/4834', 'pm_score': 0, 'selected': False, 'text': '<p>You\'re definitely between a rock and a hard place with legacy code with large dependencies.  You\'ve got a long hard slog ahead to sort it all out.</p>\n\n<p>From what you say, it seems you are trying to keep the source code intact for each module in turn, placing it in a test harness with external dependencies mocked out.  My suggestion here would be to take the even braver step of attempting some refactoring to eliminate (or <a href="http://www.objectmentor.com/resources/articles/dip.pdf" rel="nofollow noreferrer" title="Dependency Inversion Princple">invert</a>) the dependencies, which is probably the very step you are trying to avoid.</p>\n\n<p>I suggest this because I\'m guessing the dependencies are going to kill you as you write tests.  You will certainly be better off in the long term if you can eliminate the dependencies.</p>\n'}, {'answer_id': 65398, 'author': 'Ted', 'author_id': 8965, 'author_profile': 'https://Stackoverflow.com/users/8965', 'pm_score': 1, 'selected': False, 'text': "<p>I don't know if this will work for your project but\nyou might try to attack the problem from the <strong>link phase</strong> of your build.</p>\n\n<p>This would completely eliminate your #include problem.\nAll you would need to do is re-implement the interfaces in the included files to do what ever you want and then just link to the mock object files that you have created to implement the interfaces in the include file.</p>\n\n<p>The big disadvantage to this method is a more complected build system.</p>\n"}, {'answer_id': 65923, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 4, 'selected': True, 'text': '<p>The depression in the responses is overwhelming... But don\'t fear, we\'ve got <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="noreferrer" rel="nofollow noreferrer">the holy book to exorcise the demons of legacy C++ code</a>. Seriously just buy the book if you are in line for more than a week of jousting with legacy C++ code.</p>\n\n<p>Turn to page 127: <strong>The case of the horrible include dependencies.</strong> (Now I am not even within miles of Michael Feathers but here as-short-as-I-could-manage answer..)</p>\n\n<p><strong>Problem</strong>: In C++ if a classA needs to know about ClassB, Class B\'s declaration is straight-lifted / textually included in the ClassA\'s source file. And since we programmers love to take it to the wrong extreme, a file can recursively include a zillion others transitively. Builds take years.. but hey atleast it builds.. we can wait.    </p>\n\n<p>Now to say \'instantiating ClassA under a test harness is difficult\' is an understatement. (Quoting MF\'s example - Scheduler is our poster problem child with deps galore.)</p>\n\n<pre><code>#include "TestHarness.h"\n#include "Scheduler.h"\nTEST(create, Scheduler)     // your fave C++ test framework macro\n{\n  Scheduler scheduler("fred");\n}\n</code></pre>\n\n<p>This will bring out the includes dragon with a flurry of build errors.<br>\n<strong>Blow#1 Patience-n-Persistence</strong>: Take on each include one at a time and decide if we really need that dependency. Let\'s assume SchedulerDisplay is one of them, whose displayEntry method is called in Scheduler\'s ctor.<br>\n<strong>Blow#2 Fake-it-till-you-make-it</strong> (Thanks RonJ):</p>\n\n<pre><code>#include "TestHarness.h"\n#include "Scheduler.h"\nvoid SchedulerDisplay::displayEntry(const string&amp; entryDescription) {}\nTEST(create, Scheduler)\n{\n  Scheduler scheduler("fred");\n}\n</code></pre>\n\n<p>And pop goes the dependency and all its transitive includes. \nYou can also reuse the Fake methods by encapsulating it in a Fakes.h file to be included in your test files.<br>\n<strong>Blow#3 Practice</strong>: It may not be always that simple.. but you get the idea. After the first few duels, the process of breaking deps will get easy-n-mechanical</p>\n\n<p><strong>Caveats</strong> (Did I mention there are caveats? :) </p>\n\n<ul>\n<li>We need a separate build for test cases in this file ; we can have only 1 definition for the SchedulerDisplay::displayEntry method in a program. So create a separate program for scheduler tests.</li>\n<li>We aren\'t breaking any dependencies in the program, so we are not making the code cleaner.</li>\n<li>You need to maintain those fakes as long as we need the tests.</li>\n<li>Your sense of aesthetics may be offended for a while.. just bite your lip and \'bear with us for a better tomorrow\'   </li>\n</ul>\n\n<p>Use this technique for a very huge class with severe dependency issues. Don\'t use often or lightly.. <strong>Use this as a starting point for deeper refactorings.</strong> Over time this testing program can be taken behind the barn as you extract more classes (WITH their own tests).</p>\n\n<p>For more.. please do read the book. Invaluable. Fight on bro!</p>\n'}]
['https://Stackoverflow.com/questions/65074', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8908/']