Skip to content

Commit 2d5e571

Browse files
committed
Use conftest within unittesT
1 parent cc5305d commit 2d5e571

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

tests/test_loaddata.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
"""
55

66
import unittest
7+
import pytest
78

89
import numpy
910

1011
from diffpy.utils.parsers import loadData
11-
from diffpy.utils.tests.testhelpers import datafile
12-
13-
loaddata01 = datafile("loaddata01.txt")
14-
loaddatawithheaders = datafile("loaddatawithheaders.txt")
1512

1613

1714
##############################################################################
1815
class TestLoadData(unittest.TestCase):
16+
@pytest.fixture(autouse=True)
17+
def prepare_fixture(self, datafile):
18+
self.datafile = datafile
19+
1920
def test_loadData_default(self):
2021
"""check loadData() with default options"""
22+
loaddata01 = self.datafile("loaddata01.txt")
2123
d2c = numpy.array([[3, 31], [4, 32], [5, 33]])
2224
self.assertRaises(IOError, loadData, "doesnotexist")
2325
# the default minrows=10 makes it read from the third line
@@ -35,6 +37,7 @@ def test_loadData_default(self):
3537

3638
def test_loadData_1column(self):
3739
"""check loading of one-column data."""
40+
loaddata01 = self.datafile("loaddata01.txt")
3841
d1c = numpy.arange(1, 6)
3942
d = loadData(loaddata01, usecols=[0], minrows=1)
4043
self.assertTrue(numpy.array_equal(d1c, d))
@@ -46,6 +49,7 @@ def test_loadData_1column(self):
4649

4750
def test_loadData_headers(self):
4851
"""check loadData() with headers options enabled"""
52+
loaddatawithheaders = self.datafile("loaddatawithheaders.txt")
4953
hignore = ["# ", "// ", "["] # ignore lines beginning with these strings
5054
delimiter = ": " # what our data should be separated by
5155
hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)

tests/test_serialization.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55

66
from diffpy.utils.parsers import deserialize_data, loadData, serialize_data
77
from diffpy.utils.parsers.custom_exceptions import ImproperSizeError, UnsupportedTypeError
8-
from diffpy.utils.tests.testhelpers import datafile
98

109
tests_dir = os.path.dirname(os.path.abspath(locals().get("__file__", "file.py")))
1110

12-
targetjson = datafile("targetjson.json")
13-
schemaname = datafile("strumining.json")
14-
wrongtype = datafile("wrong.type")
15-
loadfile = datafile("loadfile.txt")
16-
warningfile = datafile("generatewarnings.txt")
17-
nodt = datafile("loaddatawithheaders.txt")
1811

19-
20-
def test_load_multiple(tmp_path):
21-
# generate json and apply schema
12+
def test_load_multiple(tmp_path, datafile):
13+
# Load test data
14+
targetjson = datafile("targetjson.json")
2215
generatedjson = tmp_path / "generated_serialization.json"
16+
2317
tlm_list = os.listdir(os.path.join(tests_dir, "testdata", "dbload"))
2418
tlm_list.sort()
2519
generated_data = None
@@ -50,7 +44,12 @@ def test_load_multiple(tmp_path):
5044
assert target_data == deserialize_data(generatedjson, filetype=".json")
5145

5246

53-
def test_exceptions():
47+
def test_exceptions(datafile):
48+
# Load test data
49+
wrongtype = datafile("wrong.type")
50+
loadfile = datafile("loadfile.txt")
51+
warningfile = datafile("generatewarnings.txt")
52+
nodt = datafile("loaddatawithheaders.txt")
5453
hdata = loadData(loadfile, headers=True)
5554
data_table = loadData(loadfile)
5655

@@ -107,4 +106,4 @@ def test_exceptions():
107106
)
108107
assert len(record) == 4
109108
for msg in record:
110-
assert "overwritten" in msg.message.args[0]
109+
assert "overwritten" in msg.message.args[0]

0 commit comments

Comments
 (0)