Alex Rivera | Logout

Best practices for file system dependencies in unit/integration tests

Asked 2008-12-18T10:07:08.650
21

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 and so on.

Currently the test files are stored in the test directory of the project (it's a Maven2 project) but for several reasons this directory doesn't always exist, so the tests fail.

Do you know best practices for coping with file system dependencies in unit/integration tests?

Edit: I'm not searching an answer for that specific problem I described above. That was just an example. I'd prefer general recommendations how to handle dependencies to the file system/databases etc.

Edit
Report

1 Answer

25

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.

answered 2008-12-18T10:30:12.083

Your Answer