title: Mocking a file in python
body: <p>In my python package I have a configuration module that reads a yaml file (when creating the instance) at an explicit location, i.e. something like</p>
  
  <pre><code>class YamlConfig(object):
  
      def __init__(self):
  
          filename = os.path.join(os.path.expanduser('~'), '.hanzo\\config.yml')
  
          with open(filename) as fs:
              self.cfg = yaml.load(fs.read())
  </code></pre>
  
  <p>Now what should I do when writing my unit test if I don't want to use the explicitly specified file? Instead I want to create a temporary <code>config.yml</code> to be used for testing.</p>
  
  <p>I could simply allow for a specified filename in <code>__init__()</code>, but I strongly prefer forcing the filename location. I.e. like this</p>
  
  <pre><code>class YamlConfig(object):
  
      def __init__(self, filename=os.path.join(os.path.expanduser('~'), '.hanzo\\config.yml')):
  
          with open(filename) as fs:
              self.cfg = yaml.load(fs.read())
  </code></pre>
  
  <p>Is there other ways to solve my issue? I guess it might be possible using <code>mock</code> right way? Also feel free to give any comments about upside and downside.</p>