Unit testing code with a file system dependency

前端 未结 11 753
粉色の甜心
粉色の甜心 2020-11-29 16:50

I am writing a component that, given a ZIP file, needs to:

  1. Unzip the file.
  2. Find a specific dll among the unzipped files.
  3. Load that dll thro
11条回答
  •  庸人自扰
    2020-11-29 16:59

    There's really nothing wrong with this, it's just a question of whether you call it a unit test or an integration test. You just have to make sure that if you do interact with the file system, there are no unintended side effects. Specifically, make sure that you clean up after youself -- delete any temporary files you created -- and that you don't accidentally overwrite an existing file that happened to have the same filename as a temporary file you were using. Always use relative paths and not absolute paths.

    It would also be a good idea to chdir() into a temporary directory before running your test, and chdir() back afterwards.

提交回复
热议问题