Unit Testing File I/O

There isn’t necessarily one thing to do when testing the file system. In truth, there are several things you might do, depending on the circumstances.

The question you need to ask is: What am I testing?

  • That the file system works? You probably don’t need to test that unless you’re using an operating system which you’re extremely unfamiliar with. So if you’re simply giving a command to save files, for instance, it’s a waste of time to write a test to make sure they really save.

  • That the files get saved to the right place? Well, how do you know what the right place is? Presumably you have code that combines a path with a file name. This is code you can test easily: Your input is two strings, and your output should be a string which is a valid file location constructed using those two strings.

  • That you get the right set of files from a directory? You’ll probably have to write a test for your file-getter class that really tests the file system. But you should use a test directory with files in it that won’t change. You should also put this test in an integration test project, because this is not a true unit test, because it depends on the file system.

  • But, I need to do something with the files I get. For that test, you should use a fake for your file-getter class. Your fake should return a hard-coded list of files. If you use a real file-getter and a real file-processor, you won’t know which one causes a test failure. So your file-processor class, in testing, should make use of a fake file-getter class. Your file-processor class should take the file-getter interface. In real code, you’ll pass in the real file-getter. In test code you’ll pass a fake file-getter that returns a known, static list.

The fundamental principles are:

  • Use a fake file system, hidden behind an interface, when you’re not testing the file system itself.
  • If you need to test real file operations, then
    • mark the test as an integration test, not a unit test.
    • have a designated test directory, set of files, etc. that will always be there in an unchanged state, so your file-oriented integration tests can pass consistently.

Leave a Comment