title: How do I test a method that requires a file's presence?
body: <p>For the first time, I'm using Python to create a library, and I'm trying to take the opportunity in this project to learn unit testing. I've written a first method and I want to write some unit tests for it. (Yes, I know that TDD requires I write the test first, I'll get there, really.)</p>
  
  <p>The method is fairly simple, but it expects that the class has a <code>file</code> attribute set, that the attribute points to an existing file, and that the file is an archive of some sort (currently only working with zip files, tar, rar, etc., to be added later). The method is supposed to return the number of files in the archive.</p>
  
  <p>I've created a folder in my project called <code>files</code> that contains a few sample files, and I've manually tested the method and it works as it should so far. The manual test looks like this, located in the <code>archive_file.py</code> file:</p>
  
  <pre><code>if __name__ == '__main__':
      archive = ArchiveFile()
  
      script_path = path.realpath(__file__)
      parent_dir = path.abspath(path.join(script_path, os.pardir))
      targ_dir = path.join(parent_dir, 'files')
      targ_file = path.join(targ_dir, 'test.zip' )
  
      archive.file = targ_file
  
      print(archive.file_count())
  </code></pre>
  
  <p>All I do then is make sure that what's printed is what I expect given the contents of <code>test.zip</code>.</p>
  
  <p>Here's what <code>file_count</code> looks like:</p>
  
  <pre><code>def file_count(self):
      """Return the number of files in the archive."""
      if self.file == None:
          return -1
  
      with ZipFile(self.file) as zip:
          members = zip.namelist()
          # Remove folder members if there are any.
          pruned = [item for item in members if not item.endswith('/')]
          return len(pruned)
  </code></pre>
  
  <p>Directly translating this to a unit test seems wrong to me for a few reasons, some of which may be invalid. I'm counting on the precise location of the test files in relation to the current script file, I'll need a large sample of manually created archive files to make sure I'm testing enough variations, and, of course, I'm manually comparing the returned value to what I expect because I know how many files are in the test archive.</p>
  
  <p>It <em>seems</em> to me that this should be automated as much as possible, but it also seems that doing so is going to be very complicated.</p>
  
  <p>What's the proper way to create unit tests for such a class method?</p>