r/AskProgramming • u/SendMeOrangeLetters • Mar 15 '21
Other How do I avoid going insane while unit testing shitty code with a shitty unit testing program?
There are two problems here:
The code is bad. Many variables are named value, pointer, amount... Functions are non-descriptive and it's often unclear why they are used. I'm currently testing a SD-card interfacing module, which has four functions to read the same data from the card. Why?? There are doxygen comments on the functions, but they basically just repeat the function name. Everything is a singleton, everything needs a billion functions, there is a "DataService" that deals with over a thousand variables, which are then globally accessible. There are many more problems with this code.
As much as I dislike the code, I'm actually finding more bugs in the unit testing program itself than in the code. We are using VectorCast for testing. I don't know what it does to deserve its price tag, but I'm not enjoying VectorCast. Its GUI feels clunky, it has 7 different spots for error messages and yet none of those tell me in which line or at least which function that segmentation fault occured. I can't find a way to debug other than slapping print statements on everything. VectorCasts documentation is worthless and the only way to get help is through their customer service. Maybe I wouldn't feel as bad about VectorCast if I had a colleague or anyone else to help me get into it, but I'm alone with this.
Everytime I try to write a test, I just get pissed at the code or the testing program. I keep getting distracted by this. I find myself trying to manage the explosion of code complexity by stubbing every function, but then I'm not testing the unit as a whole, I'm testing each function individually. What can I do?
8
u/Icanteven______ Mar 15 '21
Sounds like a grind. Make sure the tests you are writing are actually worth your time. Might be worth an investment in refactoring your test harness to be more user friendly if possible.
I'd take a look at the book "Working Effectively with Legacy Code" too.
5
u/wrosecrans Mar 15 '21
I haven't heard of VectorCast and can't offer specific advice about it, but I am surprised to hear of a "unit testing GUI." My basic mental model of a unit test is something super simple that can be run in CI, not something you need to run interactively. So I'd probably look into a secondary test suite of super simple tests that can run independently of this GUI. I've been fleshing out some testing on a personal project that I started a zillion years ago because I barely remember how it was supposed to work, and I have been hacking on it, and having a half dozen little EXE's that run fast and test a few things on a module has been super useful as I am hacking away at stuff.
Once you start with tests you like, the first thing you'll notice is that the thing is hard to test. So start hacking on the thing so that writing a test for a chunk of it is easier. This will force you to start reorganising some of those functions with vague responsibilities so that you can test "open a file" and "read and parse a file" and "write a file" rather than "do_misc_io_activity()"
Some of the absolutist "Test Driving Development" hardliners are annoying ideologues. But "Test Driven Refactoring" can be a pretty useful mindset because it forces you to refactor some of the internal interfaces into something meant to be used as a public interface by a test. Your program was never intended to be a library, but it is a library for the tests, so you have to imagine an interface that Joe Random Intern would be able to use to write programs with, which means clarifying why things are used, which means making things clearer. Like 90% of the work on writing a good test for a legacy codebase like this can be in hacking the library code so that writing the test is trivial. If most of the work is going into the test itself, that's a bit of a process smell.
1
u/onebit Mar 16 '21 edited Mar 16 '21
Don't test every function. Test the use case. There's probably some high level functions that you can call.
Since it wasn't written to be testable you'll probably be better off doing integration testing.
Watch tdd where did it all go wrong by ian cooper on YouTube.
24
u/balefrost Mar 15 '21
I can't really speak to #2. I've never heard of VectorCast, but then again I'm not in the embedded space.
For #1, I will always recommend Working Effectively with Legacy Code by Michael Feathers. It's all about adding tests to untested codebases, and it's basically a recipe book for "I have this problem / what are my options"?
The trick is to find good module boundaries. WEWLC calls these "seams". To add tests to an untested codebase, you need to identify and then exploit the seams that are already present. You can also perform purely mechanical but safe refactorings to introduce new seams. As you increase your test coverage, you can become bolder in the types of refactorings that you make.
You may need to erect "scaffolding" in order to get things tested, even though you might later tear down that scaffolding. That's OK.