RomaValcer RomaValcer -4 years ago 78
Python Question

How should I unit-test functions that open local files or download them?

I have a server class, that fetches files from Internet using

, and fetches files from the disk drive (
). I have several functions, whose jobs are to return an open stream, whether it's an open file, or an Internet resource. How should I unit-test these functions to know that they are returning what they should? Or, in general, how should I unit-test methods that access outside sources (whether it's a database or socket connection)?

Answer Source

You should use the mock library included in unittest.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download