r/AskProgramming • • 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:

  1. 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.

  2. 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?

70 Upvotes

6 comments sorted by

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"?


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.

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.

3

u/SendMeOrangeLetters Mar 15 '21

The trick is to find good module boundaries

That sounds good, although with some modules, I'm having trouble even understanding what the point of some functions is.

You can also perform purely mechanical but safe refactorings to introduce new seams.

I'm told to touch as little as possible and, since we are also porting this code and I don't even have access to the old hardware, I can't test any changes on the old hardware. That's a point that annoys the hell out of me too. The code has to be able to run on both the old and new hardware and now everything is littered with compiler switches because of that.

3

u/balefrost Mar 15 '21

The trick is to find good module boundaries

That sounds good, although ...

Yeah, unfortunately it's hard to provide concrete advice without talking about specific code... and you can't really share that with a stranger on the internet.

Again, I'd recommend WEWLC. It for example has a chapter "I don't understand the code well enough to change it". Admittedly, the advice it gives there isn't particularly earth shattering. It recommends literally writing on a printout of the code. It also recommends doing "scratch" refactorings where you just start changing things to see where the compiler or tests complain. The goal isn't to actually follow through with the change; the goal is to probe the codebase to uncover interdependencies that might not be apparent. In the end, you revert these scratch refactorings (after writing down the things that you learned).

Other chapters include "I can't get this class into a test harness", "I need to make a change, but I don't know what tests to write", and "my test code is in the way". Admittedly, WEWLC is generally talking about object-oriented codebases, though it does have some advice even for non-OO codebases. I don't know whether your embedded code is object-oriented or not.


I'm told to touch as little as possible...

What you're describing is unfortunately not a problem with a technical solution. "Touch as little as possible" is sometimes the right attitude, but often is indicative of a culture that doesn't value investment.

... which is strange, since your codebase apparently already has unit tests. At some point, somebody had an investment mindset. Did the people change? Or were they just trying to check a box, and never really cared about quality?

I don't even have access to the old hardware, I can't test any changes on the old hardware

That's horrifying. I'd raise this as a blocker every chance I get, and make it clear that I can't finish the changes until I am able to test on the old hardware.

Or maybe you mean that somebody has access to the old hardware, just not you. Maybe that's acceptable, but I think I'd still raise a stink.

But I don't understand. It sounds like they have an existing codebase, they need to add features to the codebase, the new code needs to run on the old hardware, but you can't test on the old hardware.

But... will the modified code even be deployed to the old hardware?

If you're not going to deploy to the old hardware, can you make the case for a clean break? Can you create a source control label or branch for "old hardware's code" (so that you can go back and make bugfixes if necessary) while freely making more sweeping changes to the current codebase?


Of course, I don't know your situation. I don't know if this is a hill that's worth dying on. If you think you're right - that the codebase needs more of a test investment and that doing so will require more changes than "touch as little as possible" would allow, then you'll need to get people on your side. You'll need to understand the desired state, understand the path to get from here to there, and then convince other people that it's worth the cost.

But if you just needed a chance to rant, I hope this was useful too.

Still, get yourself a copy of WEWLC. Heck, ask your employer to buy you a copy. Maybe they'll get the message.

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.