Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for file system dependencies in unit/integration tests

Tags:

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.

like image 761
Olvagor Avatar asked Dec 18 '08 10:12

Olvagor


2 Answers

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.

like image 137
philant Avatar answered Oct 15 '22 18:10

philant


Dependencies on the filesystem come in two flavours here:

  • files that your tests depend upon; if you need files to run the test, then you can generate them in your tests and put them in a /tmp directory.
  • files that your code is dependent upon: config files, or input files.

In this second case, it is often possible to re-structure your code to remove dependency on a file (e.g. java.io.File can be replaced with java.io.InputStream and java.io.OutputStream, etc.) This may not be possible of course.

You may also need to handle 'non-determinism' in the filesystem (I had a devil of a job debugging something on an NFS once). In this case you should probably wrap the file system in a thin interface.

At its simplest, this is just helper methods that take a File and forward the call onto that file:

InputStream getInputStream(File file) throws IOException {     return new FileInputStream(file); } 

You can then replace this one with a mock which you can direct to throw the exception, or return a ByteArrayInputStream, or whatever.

The same can be said for URLs and URIs.

like image 33
jamesh Avatar answered Oct 15 '22 18:10

jamesh