Reputation: 229
This is more a conceptual question. I am unit testing a script I wrote that has multiple modules. I have a main.py and a formatting.py. My coverage is at 100% for formatting but my main.py is at 30%. In my main, I just call all of the functions inside formatting. Do i need to just test them again in main directly? This seems like a waste of time? Maybe I am not understanding correctly. Thanks in advance
Upvotes: 1
Views: 204
Reputation: 147
The fact that internal pieces are tested allows you to capitalize on it by reducing the number of the tests required for the new, higher level method.
There always will be some redundancy between those, but decent and straightforward seams would allow you to have a very few tests for the new level.
The guys in the other answers are talking about considering those tests as integrational, but I would claim that you also would need some tests in isolation for the upper function itself with all already tested dependencies being mocked and excluded. It's not always necessary, but please be aware that you end up with a mixed test otherwise, since there's a new functionality on the top level.
Upvotes: 1
Reputation: 72251
A good rule of thumb for unit testing is to:
But how will you know the program works then? Well, how about some integration tests? For example, if you're writing a command line script, an integration test might run it the whole script with some inputs and check if the script did the right thing, without even considering how it is structured.
Depending on your needs and the size of the script, you might decide to have unit tests, integration tests or both.
Upvotes: 1
Reputation: 2353
Usually the trick is to test what external function does by itself and not other functions that are called and are already tested. This is not a problem if internal functions are actually called, just avoid testing them again.
If you want to avoid calling internal functions though, you may try dependecy injection or mocking.
If your external function is simple enough, you may forgo testing it, 100% coverage is not a rule.
Upvotes: 1