note
ait
<blockquote>
If code needs to know that it is being tested, I suspect something wrong with the interface.
</blockquote>
<p>
Probably so, and in theory everything sound nice, but in real-world every-day coding, with a team of coders, and with time and budget constraints, this is by far the common case. You need to be able to test things while skipping others that don't concern that particular aspect that you are working on or testing.
</p>
<p>
Example: you are testing a class that invokes an external service to send an SMS, and the SMS gateway has no test mode, so it costs you money. Your class does a hundred things besides sending the SMS and has a lot of business logic and you only want to skip the SMS sending part to test this logic. Maybe the SMS sending logic was encapsulated in a class (in which case could be mocked) or maybe it's part of the same class on a sub and can't be mocked. Like this one, there are a many day-to-day scenarios where you want to skip over some part of the code just o test the overall logic.
</p>
<p>
Refactoring to make code perfectly testable is not always an option, in fact, it rarely is, at least in large projects. The code is far from perfect and you have to make the best of it and try to test it as best you can within the time and budget constraints that a particular project allows.
</p>
982132
982237