Skip to content

Commit b5042f6

Browse files
use tmp_path in tests that create temporary files
1 parent 044245a commit b5042f6

File tree

5 files changed

+219
-231
lines changed

5 files changed

+219
-231
lines changed

tests/test_airglow.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44

55

6-
def test_find_airglow_limits():
6+
def test_find_airglow_limits(tmp_path):
77
"""
88
unit test for find_airglow_limits()
99
test ran
@@ -20,13 +20,23 @@ def test_find_airglow_limits():
2020
2121
"""
2222
# Setup
23-
inf = {"obstype": "SPECTROSCOPIC", "cenwave": 1055, "aperture": "PSA", "detector": "FUV",
24-
"opt_elem": "G130M", "segment": "FUVA"}
23+
inf = {
24+
"obstype": "SPECTROSCOPIC",
25+
"cenwave": 1055,
26+
"aperture": "PSA",
27+
"detector": "FUV",
28+
"opt_elem": "G130M",
29+
"segment": "FUVA",
30+
}
2531
seg = ["FUVA", "FUVB"]
26-
disptab = create_disptab_file('49g17153l_disp.fits')
32+
disptab = create_disptab_file(str(tmp_path / "49g17153l_disp.fits"))
2733
airglow_lines = ["Lyman_alpha", "N_I_1200", "O_I_1304", "O_I_1356", "N_I_1134"]
2834
actual_pxl = [
29-
[], [], (15421.504705213156, 15738.02214190493), (8853.838672375898, 9135.702216258482)]
35+
[],
36+
[],
37+
(15421.504705213156, 15738.02214190493),
38+
(8853.838672375898, 9135.702216258482),
39+
]
3040
# Test
3141
test_pxl = [[], []]
3242
# only works for FUV
@@ -39,6 +49,3 @@ def test_find_airglow_limits():
3949
# Verify
4050
for i in range(len(actual_pxl)):
4151
assert actual_pxl[i] == test_pxl[i]
42-
43-
# Cleanup
44-
os.remove(disptab)

tests/test_average.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from generate_tempfiles import create_count_file
88

99

10-
def test_avg_image():
10+
def test_avg_image(tmp_path):
1111
"""
1212
tests avg_image() in average.py
1313
explanation of the test
@@ -19,10 +19,8 @@ def test_avg_image():
1919
pass if expected == actual fail otherwise.
2020
"""
2121
# Setup
22-
infile = ["test_count1.fits", "test_count2.fits"]
23-
outfile = "test_output.fits"
24-
if os.path.exists(outfile):
25-
os.remove(outfile) # avoid file exists error
22+
infile = [str(tmp_path / "test_count1.fits"), str(tmp_path / "test_count2.fits")]
23+
outfile = str(tmp_path / "test_output.fits")
2624
create_count_file(infile[0])
2725
create_count_file(infile[1])
2826
inhdr1, inhdr2 = fits.open(infile[0]), fits.open(infile[1])
@@ -32,11 +30,8 @@ def test_avg_image():
3230

3331
# Verify
3432
assert os.path.exists(outfile)
35-
for (i, j, k) in zip(inhdr1[1].header, inhdr2[1].header, out_hdr[1].header):
33+
for i, j, k in zip(inhdr1[1].header, inhdr2[1].header, out_hdr[1].header):
3634
assert i == j == k
37-
np.testing.assert_array_equal((inhdr1[1].data + inhdr1[1].data) / 2, out_hdr[1].data)
38-
39-
# Cleanup
40-
for tempfile in infile:
41-
os.remove(tempfile)
42-
os.remove(outfile)
35+
np.testing.assert_array_equal(
36+
(inhdr1[1].data + inhdr1[1].data) / 2, out_hdr[1].data
37+
)

0 commit comments

Comments
 (0)