in Education by
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. 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. Anyone know some better practices? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The depression in the responses is overwhelming... But don't fear, we've got the holy book to exorcise the demons of legacy C++ code. Seriously just buy the book if you are in line for more than a week of jousting with legacy C++ code. Turn to page 127: The case of the horrible include dependencies. (Now I am not even within miles of Michael Feathers but here as-short-as-I-could-manage answer..) Problem: 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. 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.) #include "TestHarness.h" #include "Scheduler.h" TEST(create, Scheduler) // your fave C++ test framework macro { Scheduler scheduler("fred"); } This will bring out the includes dragon with a flurry of build errors. Blow#1 Patience-n-Persistence: 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. Blow#2 Fake-it-till-you-make-it (Thanks RonJ): #include "TestHarness.h" #include "Scheduler.h" void SchedulerDisplay::displayEntry(const string& entryDescription) {} TEST(create, Scheduler) { Scheduler scheduler("fred"); } And pop goes the dependency and all its transitive includes. You can also reuse the Fake methods by encapsulating it in a Fakes.h file to be included in your test files. Blow#3 Practice: 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 Caveats (Did I mention there are caveats? :) 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. We aren't breaking any dependencies in the program, so we are not making the code cleaner. You need to maintain those fakes as long as we need the tests. Your sense of aesthetics may be offended for a while.. just bite your lip and 'bear with us for a better tomorrow' Use this technique for a very huge class with severe dependency issues. Don't use often or lightly.. Use this as a starting point for deeper refactorings. Over time this testing program can be taken behind the barn as you extract more classes (WITH their own tests). For more.. please do read the book. Invaluable. Fight on bro!

Related questions

0 votes
    I've just started writing unit tests for a legacy code module with large physical dependencies using the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    How does one write a unittest that fails only if a function doesn't throw an expected exception? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    - What will be the output of the following C code? #include int main() { int i = 97, *p=&i; foo( ... (D) Segmentation fault/code crash Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
0 votes
    I have written a piece of code to validate a keyword, it validates and makes sure that the word is 5 letters long and ... the problem, as it works fine without it. The code: cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    In a simple linear regression model (One independent variable), If we change the input variable by 1 unit. How ... of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I have a C library compiled with MinGW, Eclipse C project. Library consists of many modules, each module ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    There are two strings in a program. Each of them contains a path to some file or folder. How can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    There are two strings in a program. Each of them contains a path to some file or folder. How can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    There are two strings in a program. Each of them contains a path to some file or folder. How can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Arrange in order- A. running unit test B. running static code check C. check for code D. Build Package...
asked Mar 7, 2021 in Technology by JackTerrance
0 votes
    With all the fuss about opensource projects, how come there is still not a strong standard that enables you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    Locked. This question and its answers are locked because the question is off-topic but has historical ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I want to improve the next code, calculating the mean: void calculateMeanStDev8x8Aux(cv::Mat* patch, int sx, int sy, int& ... the next loop with NEON intrinsics: for (int i=0; i...
asked Feb 18, 2022 in Education by JackTerrance
...