Best practices for file system dependencies in unit/integration tests

前端 未结 5 677
星月不相逢
星月不相逢 2020-12-29 05:29

I just started writing tests for a lot of code. There\'s a bunch of classes with dependencies to the file system, that is they read CSV files, read/write configuration files

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 05:54

    First one should try to keep the unit tests away from the filesystem - see this Set of Unit Testing Rules. If possible have your code working with Streams that will be buffers (i.e. in memory) for the unit tests, and FileStream in the production code.

    If this is not feasible, you can have your unit tests generates the files they need. This makes the test easy to read as everything is in one file. This may also prevent permissions problem.

    You can mock the filesystem/database/network access in your unit tests.

    You can consider the unit tests that rely on DB or file systems as integration tests.

提交回复
热议问题