From: Ole Streicher <olebole@debian.org>
Date: Fri, 18 Sep 2020 16:23:11 +0200
Subject: Write test output to temporary file

URL: https://github.com/radio-astro-tools/spectral-cube/issues/451

(modified for 0.5.0)
---
 spectral_cube/tests/test_wcs_utils.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/spectral_cube/tests/test_wcs_utils.py b/spectral_cube/tests/test_wcs_utils.py
index d832aeb..cf0aeb4 100644
--- a/spectral_cube/tests/test_wcs_utils.py
+++ b/spectral_cube/tests/test_wcs_utils.py
@@ -1,5 +1,7 @@
 import pytest
 import warnings
+import os
+import tempfile
 
 from astropy.io import fits
 import numpy as np
@@ -154,16 +156,17 @@ def test_strip_wcs(fn):
     with open(path(fn),'r') as fh:
         hdrlines = fh.readlines()
 
-    newfn = fn.replace('.hdr', '_blanks.hdr')
+    fd, newfn = tempfile.mkstemp(suffix='.hdr')
 
     hdrlines.insert(-20,"\n")
     hdrlines.insert(-1,"\n")
-    with open(path(newfn),'w') as fh:
+    with os.fdopen(fd,'w') as fh:
         fh.writelines(hdrlines)
 
     header2 = fits.Header.fromtextfile(path(newfn))
     header2_stripped = strip_wcs_from_header(header2)
 
+    os.remove(newfn)
     assert header1_stripped == header2_stripped
 
 def test_wcs_slice_unmatched_celestial():
