Skip to content

Commit d4977c0

Browse files
author
Benjamin Moody
committed
Write test output files to a temporary directory.
When writing files as part of test cases, write them to a temporary directory (created in setUpClass and destroyed in tearDownClass) rather than writing them to the current working directory. This allows running tests without modifying the working tree, which means multiple test processes can run simultaneously from the same working tree (e.g., using pytest-xdist).
1 parent f697e59 commit d4977c0

File tree

2 files changed

+104
-87
lines changed

2 files changed

+104
-87
lines changed

tests/test_annotation.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import tempfile
34
import unittest
45

56
import numpy as np
@@ -85,9 +86,11 @@ def test_1(self):
8586
pn_annotation.create_label_map()
8687

8788
# Test file writing
88-
annotation.wrann(write_fs=True)
89+
annotation.wrann(write_fs=True, write_dir=self.temp_path)
8990
write_annotation = wfdb.rdann(
90-
"100", "atr", return_label_elements=["label_store", "symbol"]
91+
os.path.join(self.temp_path, "100"),
92+
"atr",
93+
return_label_elements=["label_store", "symbol"],
9194
)
9295
write_annotation.create_label_map()
9396

@@ -156,9 +159,11 @@ def test_2(self):
156159
pn_annotation.create_label_map()
157160

158161
# Test file writing
159-
annotation.wrann(write_fs=True)
162+
annotation.wrann(write_fs=True, write_dir=self.temp_path)
160163
write_annotation = wfdb.rdann(
161-
"12726", "anI", return_label_elements=["label_store", "symbol"]
164+
os.path.join(self.temp_path, "12726"),
165+
"anI",
166+
return_label_elements=["label_store", "symbol"],
162167
)
163168
write_annotation.create_label_map()
164169

@@ -228,9 +233,11 @@ def test_3(self):
228233
pn_annotation.create_label_map()
229234

230235
# Test file writing
231-
annotation.wrann(write_fs=True)
236+
annotation.wrann(write_fs=True, write_dir=self.temp_path)
232237
write_annotation = wfdb.rdann(
233-
"1003", "atr", return_label_elements=["label_store", "symbol"]
238+
os.path.join(self.temp_path, "1003"),
239+
"atr",
240+
return_label_elements=["label_store", "symbol"],
234241
)
235242
write_annotation.create_label_map()
236243

@@ -247,10 +254,10 @@ def test_4(self):
247254
"""
248255
annotation = wfdb.rdann("sample-data/huge", "qrs")
249256
self.assertEqual(annotation.sample[0], 10000000000)
250-
annotation.wrann()
257+
annotation.wrann(write_dir=self.temp_path)
251258

252259
annotation1 = wfdb.rdann("sample-data/huge", "qrs")
253-
annotation2 = wfdb.rdann("huge", "qrs")
260+
annotation2 = wfdb.rdann(os.path.join(self.temp_path, "huge"), "qrs")
254261
self.assertEqual(annotation1, annotation2)
255262

256263
def test_5(self):
@@ -274,22 +281,19 @@ def test_5(self):
274281
chan=ann_chan,
275282
custom_labels=ann_custom_labels,
276283
label_store=ann_label_store,
284+
write_dir=self.temp_path,
277285
)
278-
ann = wfdb.rdann("CustomLabel", "atr")
286+
ann = wfdb.rdann(os.path.join(self.temp_path, "CustomLabel"), "atr")
279287
self.assertEqual(ann.symbol, ["z", "l", "v", "r"])
280288

289+
@classmethod
290+
def setUpClass(cls):
291+
cls.temp_directory = tempfile.TemporaryDirectory()
292+
cls.temp_path = cls.temp_directory.name
293+
281294
@classmethod
282295
def tearDownClass(cls):
283-
writefiles = [
284-
"100.atr",
285-
"1003.atr",
286-
"12726.anI",
287-
"huge.qrs",
288-
"CustomLabel.atr",
289-
]
290-
for file in writefiles:
291-
if os.path.isfile(file):
292-
os.remove(file)
296+
cls.temp_directory.cleanup()
293297

294298

295299
if __name__ == "__main__":

tests/test_record.py

Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import os
33
import shutil
4+
import tempfile
45
import unittest
56

67
import numpy as np
@@ -44,9 +45,11 @@ def test_1a(self):
4445
"sample-data/test01_00s", physical=False, return_res=16
4546
)
4647
record_2.sig_name = ["ECG_1", "ECG_2", "ECG_3", "ECG_4"]
47-
record_2.wrsamp()
48+
record_2.wrsamp(write_dir=self.temp_path)
4849
record_write = wfdb.rdrecord(
49-
"test01_00s", physical=False, return_res=16
50+
os.path.join(self.temp_path, "test01_00s"),
51+
physical=False,
52+
return_res=16,
5053
)
5154

5255
assert np.array_equal(sig, sig_target)
@@ -121,9 +124,11 @@ def test_1c(self):
121124
)
122125

123126
# Test file writing
124-
record.wrsamp(expanded=True)
127+
record.wrsamp(write_dir=self.temp_path, expanded=True)
125128
record_write = wfdb.rdrecord(
126-
"a103l", physical=False, smooth_frames=False
129+
os.path.join(self.temp_path, "a103l"),
130+
physical=False,
131+
smooth_frames=False,
127132
)
128133

129134
assert np.array_equal(sig, sig_target)
@@ -180,8 +185,11 @@ def test_1e(self):
180185
record_2 = wfdb.rdrecord(
181186
"sample-data/n8_evoked_raw_95_F1_R9", physical=False
182187
)
183-
record_2.wrsamp()
184-
record_write = wfdb.rdrecord("n8_evoked_raw_95_F1_R9", physical=False)
188+
record_2.wrsamp(write_dir=self.temp_path)
189+
record_write = wfdb.rdrecord(
190+
os.path.join(self.temp_path, "n8_evoked_raw_95_F1_R9"),
191+
physical=False,
192+
)
185193

186194
assert np.array_equal(sig, sig_target)
187195
assert record.__eq__(record_pn)
@@ -251,8 +259,11 @@ def test_read_write_flac(self):
251259
)
252260

253261
# Test file writing
254-
record.wrsamp()
255-
record_write = wfdb.rdrecord("flacformats", physical=False)
262+
record.wrsamp(write_dir=self.temp_path)
263+
record_write = wfdb.rdrecord(
264+
os.path.join(self.temp_path, "flacformats"),
265+
physical=False,
266+
)
256267
assert record == record_write
257268

258269
def test_read_write_flac_multifrequency(self):
@@ -265,11 +276,14 @@ def test_read_write_flac_multifrequency(self):
265276
physical=False,
266277
smooth_frames=False,
267278
)
268-
record.wrsamp(expanded=True)
279+
record.wrsamp(write_dir=self.temp_path, expanded=True)
269280

270281
# Check that result matches the original
271282
record = wfdb.rdrecord("sample-data/mixedsignals", smooth_frames=False)
272-
record_write = wfdb.rdrecord("mixedsignals", smooth_frames=False)
283+
record_write = wfdb.rdrecord(
284+
os.path.join(self.temp_path, "mixedsignals"),
285+
smooth_frames=False,
286+
)
273287
assert record == record_write
274288

275289
def test_read_flac_longduration(self):
@@ -358,8 +372,11 @@ def test_2b(self):
358372
del record_named.comments[0]
359373

360374
# Test file writing
361-
record.wrsamp()
362-
record_write = wfdb.rdrecord("100", physical=False)
375+
record.wrsamp(write_dir=self.temp_path)
376+
record_write = wfdb.rdrecord(
377+
os.path.join(self.temp_path, "100"),
378+
physical=False,
379+
)
363380

364381
assert np.array_equal(sig, sig_target)
365382
assert record.__eq__(record_pn)
@@ -379,8 +396,8 @@ def test_2c(self):
379396

380397
# Test file writing
381398
record.d_signal = record.adc()
382-
record.wrsamp()
383-
record_write = wfdb.rdrecord("100_3chan")
399+
record.wrsamp(write_dir=self.temp_path)
400+
record_write = wfdb.rdrecord(os.path.join(self.temp_path, "100_3chan"))
384401
record.d_signal = None
385402

386403
assert np.array_equal(sig_round, sig_target)
@@ -434,8 +451,11 @@ def test_3a(self):
434451
)
435452

436453
# Test file writing
437-
record.wrsamp()
438-
record_write = wfdb.rdrecord("s0010_re", physical=False)
454+
record.wrsamp(write_dir=self.temp_path)
455+
record_write = wfdb.rdrecord(
456+
os.path.join(self.temp_path, "s0010_re"),
457+
physical=False,
458+
)
439459

440460
assert np.array_equal(sig, sig_target)
441461
assert record.__eq__(record_pn)
@@ -497,9 +517,12 @@ def test_4a(self):
497517
smooth_frames=False,
498518
ignore_skew=True,
499519
)
500-
record_no_skew.wrsamp(expanded=True)
520+
record_no_skew.wrsamp(write_dir=self.temp_path, expanded=True)
501521
# Read the written record
502-
record_write = wfdb.rdrecord("test01_00s_skewframe", physical=False)
522+
record_write = wfdb.rdrecord(
523+
os.path.join(self.temp_path, "test01_00s_skewframe"),
524+
physical=False,
525+
)
503526

504527
assert np.array_equal(sig, sig_target)
505528
assert record.__eq__(record_write)
@@ -533,9 +556,12 @@ def test_4b(self):
533556
smooth_frames=False,
534557
ignore_skew=True,
535558
)
536-
record_no_skew.wrsamp(expanded=True)
559+
record_no_skew.wrsamp(write_dir=self.temp_path, expanded=True)
537560
# Read the written record
538-
record_write = wfdb.rdrecord("03700181", physical=False)
561+
record_write = wfdb.rdrecord(
562+
os.path.join(self.temp_path, "03700181"),
563+
physical=False,
564+
)
539565

540566
assert np.array_equal(sig, sig_target)
541567
assert record.__eq__(record_pn)
@@ -572,10 +598,13 @@ def test_4c(self):
572598
smooth_frames=False,
573599
ignore_skew=True,
574600
)
575-
record_no_skew.wrsamp(expanded=True)
601+
record_no_skew.wrsamp(write_dir=self.temp_path, expanded=True)
576602
# Read the written record
577603
writesig, writefields = wfdb.rdsamp(
578-
"03700181", channels=[0, 2], sampfrom=1000, sampto=16000
604+
os.path.join(self.temp_path, "03700181"),
605+
channels=[0, 2],
606+
sampfrom=1000,
607+
sampto=16000,
579608
)
580609

581610
assert np.array_equal(sig_round, sig_target)
@@ -639,38 +668,13 @@ def test_header_with_non_utf8(self):
639668
assert record.units.__eq__(sig_units_target)
640669

641670
@classmethod
642-
def tearDownClass(cls):
643-
"Clean up written files"
644-
writefiles = [
645-
"03700181.dat",
646-
"03700181.hea",
647-
"100.dat",
648-
"100.hea",
649-
"100_3chan.dat",
650-
"100_3chan.hea",
651-
"a103l.hea",
652-
"a103l.mat",
653-
"flacformats.d0",
654-
"flacformats.d1",
655-
"flacformats.d2",
656-
"flacformats.hea",
657-
"mixedsignals.hea",
658-
"mixedsignals_e.dat",
659-
"mixedsignals_p.dat",
660-
"mixedsignals_r.dat",
661-
"s0010_re.dat",
662-
"s0010_re.hea",
663-
"s0010_re.xyz",
664-
"test01_00s.dat",
665-
"test01_00s.hea",
666-
"test01_00s_skewframe.hea",
667-
"n8_evoked_raw_95_F1_R9.dat",
668-
"n8_evoked_raw_95_F1_R9.hea",
669-
]
671+
def setUpClass(cls):
672+
cls.temp_directory = tempfile.TemporaryDirectory()
673+
cls.temp_path = cls.temp_directory.name
670674

671-
for file in writefiles:
672-
if os.path.isfile(file):
673-
os.remove(file)
675+
@classmethod
676+
def tearDownClass(cls):
677+
cls.temp_directory.cleanup()
674678

675679

676680
class TestMultiRecord(unittest.TestCase):
@@ -1062,11 +1066,18 @@ def test_physical_conversion(self):
10621066
adc_gain=adc_gain,
10631067
baseline=baseline,
10641068
fmt=["16", "16", "16"],
1069+
write_dir=self.temp_path,
1070+
)
1071+
record = wfdb.rdrecord(
1072+
os.path.join(self.temp_path, "test_physical_conversion"),
1073+
physical=False,
10651074
)
1066-
record = wfdb.rdrecord("test_physical_conversion", physical=False)
10671075
np.testing.assert_array_equal(record.d_signal, d_signal)
10681076

1069-
record = wfdb.rdrecord("test_physical_conversion", physical=True)
1077+
record = wfdb.rdrecord(
1078+
os.path.join(self.temp_path, "test_physical_conversion"),
1079+
physical=True,
1080+
)
10701081
for ch, gain in enumerate(adc_gain):
10711082
np.testing.assert_allclose(
10721083
record.p_signal[:, ch],
@@ -1075,32 +1086,34 @@ def test_physical_conversion(self):
10751086
atol=(0.05 / gain),
10761087
)
10771088

1089+
@classmethod
1090+
def setUpClass(cls):
1091+
cls.temp_directory = tempfile.TemporaryDirectory()
1092+
cls.temp_path = cls.temp_directory.name
1093+
10781094
@classmethod
10791095
def tearDownClass(cls):
1080-
writefiles = [
1081-
"test_physical_conversion.dat",
1082-
"test_physical_conversion.hea",
1083-
]
1084-
for file in writefiles:
1085-
if os.path.isfile(file):
1086-
os.remove(file)
1096+
cls.temp_directory.cleanup()
10871097

10881098

10891099
class TestDownload(unittest.TestCase):
10901100
# Test that we can download records with no "dat" file
10911101
# Regression test for https://github.com/MIT-LCP/wfdb-python/issues/118
10921102
def test_dl_database_no_dat_file(self):
1093-
wfdb.dl_database("afdb", "./download-tests/", ["00735"])
1103+
wfdb.dl_database("afdb", self.temp_path, ["00735"])
10941104

10951105
# Test that we can download records that *do* have a "dat" file.
10961106
def test_dl_database_with_dat_file(self):
1097-
wfdb.dl_database("afdb", "./download-tests/", ["04015"])
1107+
wfdb.dl_database("afdb", self.temp_path, ["04015"])
1108+
1109+
@classmethod
1110+
def setUpClass(cls):
1111+
cls.temp_directory = tempfile.TemporaryDirectory()
1112+
cls.temp_path = cls.temp_directory.name
10981113

1099-
# Cleanup written files
11001114
@classmethod
1101-
def tearDownClass(self):
1102-
if os.path.isdir("./download-tests/"):
1103-
shutil.rmtree("./download-tests/")
1115+
def tearDownClass(cls):
1116+
cls.temp_directory.cleanup()
11041117

11051118

11061119
if __name__ == "__main__":

0 commit comments

Comments
 (0)