Skip to content

Commit 6b441ad

Browse files
authored
Merge pull request #96 from bobleesj/move-folder
Move tests folder to top and use pytest to run all tests
2 parents aa21d21 + 19a7da8 commit 6b441ad

23 files changed

+36
-170
lines changed

pytest.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
[pytest]
2-
testpaths= src/diffpy/utils/tests
3-
python_files = test_*.py

src/diffpy/utils/tests/__init__.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/diffpy/utils/tests/debug.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/diffpy/utils/tests/run.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/__init__.py

Whitespace-only changes.

src/diffpy/utils/tests/conftest.py renamed to tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from importlib.resources import as_file, files
23
from pathlib import Path
34

45
import pytest
@@ -17,3 +18,20 @@ def user_filesystem(tmp_path):
1718
json.dump(home_config_data, f)
1819

1920
yield tmp_path
21+
22+
23+
def get_datafile(filename):
24+
"""Helper function to retrieve the file path for test data."""
25+
ref = files(__package__) / f"testdata/{filename}"
26+
with as_file(ref) as rv:
27+
return rv
28+
29+
30+
@pytest.fixture
31+
def datafile():
32+
"""Fixture to dynamically load any test file."""
33+
34+
def _load(filename):
35+
return get_datafile(filename)
36+
37+
return _load
File renamed without changes.

src/diffpy/utils/tests/test_loaddata.py renamed to tests/test_loaddata.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
import unittest
77

88
import numpy
9+
import pytest
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)
File renamed without changes.

src/diffpy/utils/tests/test_serialization.py renamed to tests/test_serialization.py

Lines changed: 10 additions & 11 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

0 commit comments

Comments
 (0)