Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Unless you are testing a file utility class, if you access files during your test, you exceeding the scope of the unit that you should be testing

That statement sums up the principle behind this rule. However, there are more pragmatic reasons to consider as well.

Clarity / Evident Data

Often the motivation for accessing files from a unit test is to read test data upon which the class under test depends. Separating test data from the code of your test makes that test significantly harder to understand and debug. Kent Beck refers to this unit testing best practice as Evident Data. Show the reader of your test the intent of the code being tested by using clear and understandable data as parameters. Once you move that data into a separate file, especiall in binary formats, the maintenance cost and usefulness of that test becomes significantly worse.

Speed Of Execution

Reading files is much slower than executing code.

Ease of Setup and Brittleness Issues

Unit tests that depend on files being in the right place in order to pass

  1. Some of your own code in your SUT was trying to do file I/O
  2. Your test was trying to do file I/O
  3. Some code that your SUT or test is using was trying to do file I/O
  • No labels