Reputation: 171
let's say that I have a file called boop.py
that reads:
import module_a
import sys
def boop(value):
val = module_a.boop_it(value)
# ...
return val
My questions are:
when I'm doing the tests for this, how can I make module_a
be a dummy module?
I understand I need to isolate the dependencies. I just don't understand the mechanics of it
Is it necessary to isolate it entirely? If I don't, will it come to bite me?
Note that this is all legacy code, that already exists and has years of history
Upvotes: 0
Views: 668
Reputation: 5939
To answer your second question "Is it necessary to isolate it entirely?": This depends. Even if you are doing unit-testing, you typically do not have to isolate your code from all dependencies. For example, you would not isolate your code from math.sin()
. I would even say, the creation of test doubles should be avoided except there is a reason. In practice, however, often enough there is a reason.
Here are some criteria that can help you decide for your module_a
whether the dependency is troubling you when unit-testing. They all relate to the properties of the depended-on-component ("DOC", in your case the module_a
) including its transitive dependencies and on your testing goals:
But, even if the criteria above indicate that the dependency is troubling you: Keep in mind that some re-design of the code may be preferrable to creating test doubles. For example, separating computations from interactions by putting each into different functions can save you from some mocking: You test the computations with unit-testing and the interactions with integration-testing.
Upvotes: 1