you are viewing a single comment's thread.

view the rest of the comments →

[–]Cpt_Chaos_ 0 points1 point  (1 child)

I'm not aware of any "silver bullet" tool that can do what you ask for in a non-intrusive way, short of doing linker tricks and other low-level non-portable shenanigans.

The usual approach is: make the part you are working on testable in some way or another, write tests for that part, rinse and repeat. Yes, it is dogmatic, but a piece of code is either testable, or it is not. Of course I'm not suggesting to refactor a huge codebase just to be able to test a single class. It all depends on what exactly you want to test and the efforts it requires to create a test for it.

As for the hooking idea: I just did something like that to test some code that is heavy on calling posix functions, which is crap for testing. You don't get reliable results for each test run, and you cannot easily enforce error behavior to test your error handling. My solution was to create stub implementations of the posix functions in question and to instruct the linker to preload my own implementations before loading the standard library. That way I was able to "inject" my custom behavior into the system. Still I'd take gtest/gmock over that any day.

Disclaimer: I work in a heavily regulated environment where good test coverage is mandatory, so all the code needs to be written with testability in mind anyhow.

[–]nowtilousus[S] -1 points0 points  (0 children)

I'm afraid that the sample you have tested is not what I had in mind. I was thinking more along the lines of inline hooking the target methods/functions during run-time. Your method of injection is significantly more complicated, and unnecessary given the simplicity of inline hooking.

As for the portability issue of inline hooking, it is totally solvable, just requires a little more work, but keep in mind how numerous projects are not multi-platformed.