Skip to content

Monthly Archives: May 2012

Stubbing out open() in Python

Here’s an example that stubs out the open() function in Python using the mock library. from StringIO import StringIO from mock import patch # We are testing these 2 functions : def read_sys(): f = open(‘/sys/something’) data = f.readline().strip() f.close() return data def read_dev(): f = open(‘/dev/something’) data = f.readline().strip() f.close() return data # Test […]