Improved opening/closing of files in HDF5 tests

This follows @mdboom's suggestion from #677. This can be merged as soon as the Travis tests pass.