Skip to content

Commit 5f34e62

Browse files
authored
Merge pull request #46 from Tieqiong/test
refactor: move tests and fix pytest
2 parents 4bf46d2 + 0ce4864 commit 5f34e62

38 files changed

+174
-275
lines changed

Diff for: .github/workflows/tests-on-pr.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ jobs:
3636
- name: Install diffpy.srreal and requirements
3737
run: |
3838
conda install --file requirements/test.txt
39-
conda install --file requirements/conda.txt
39+
conda install boost numpy libdiffpy setuptools diffpy.structure periodictable gsl
4040
python -m pip install . --no-deps
4141
4242
- name: Validate diffpy.pdfgui
43-
run: python -m diffpy.srreal.tests.run
43+
run: pytest tests

Diff for: src/diffpy/srreal/tests/__init__.py

-85
This file was deleted.

Diff for: src/diffpy/srreal/tests/debug.py

-33
This file was deleted.

Diff for: src/diffpy/srreal/tests/run.py

-37
This file was deleted.

Diff for: tests/conftest.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import json
2+
import logging
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from diffpy.srreal.structureconverters import convertObjCrystCrystal
8+
9+
10+
@pytest.fixture
11+
def user_filesystem(tmp_path):
12+
base_dir = Path(tmp_path)
13+
home_dir = base_dir / "home_dir"
14+
home_dir.mkdir(parents=True, exist_ok=True)
15+
cwd_dir = base_dir / "cwd_dir"
16+
cwd_dir.mkdir(parents=True, exist_ok=True)
17+
18+
home_config_data = {"username": "home_username", "email": "[email protected]"}
19+
with open(home_dir / "diffpyconfig.json", "w") as f:
20+
json.dump(home_config_data, f)
21+
22+
yield tmp_path
23+
24+
25+
# Resolve availability of optional packages.
26+
27+
# pyobjcryst
28+
29+
30+
@pytest.fixture(scope="session")
31+
def _msg_nopyobjcryst():
32+
return "No module named 'pyobjcryst'"
33+
34+
35+
@pytest.fixture(scope="session")
36+
def has_pyobjcryst():
37+
try:
38+
import pyobjcryst.crystal
39+
40+
convertObjCrystCrystal(pyobjcryst.crystal.Crystal())
41+
has_pyobjcryst = True
42+
except ImportError:
43+
has_pyobjcryst = False
44+
logging.warning("Cannot import pyobjcryst, pyobjcryst tests skipped.")
45+
print("Cannot import pyobjcryst, pyobjcryst tests skipped.")
46+
except TypeError:
47+
has_pyobjcryst = False
48+
logging.warning("Compiled without ObjCryst, pyobjcryst tests skipped.")
49+
print("Compiled without ObjCryst, pyobjcryst tests skipped.")
50+
51+
return has_pyobjcryst
52+
53+
54+
# periodictable
55+
56+
57+
@pytest.fixture(scope="session")
58+
def _msg_noperiodictable():
59+
return "No module named 'periodictable'"
60+
61+
62+
@pytest.fixture(scope="session")
63+
def has_periodictable():
64+
try:
65+
import periodictable
66+
67+
has_periodictable = True
68+
69+
# silence the pyflakes syntax checker
70+
del periodictable
71+
except ImportError:
72+
has_periodictable = False
73+
logging.warning("Cannot import periodictable, periodictable tests skipped.")
74+
75+
return has_periodictable

Diff for: src/diffpy/srreal/tests/testatomradiitable.py renamed to tests/test_atomradiitable.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import pickle
77
import unittest
88

9+
import pytest
10+
911
from diffpy.srreal.atomradiitable import AtomRadiiTable, ConstantRadiiTable, CovalentRadiiTable
10-
from diffpy.srreal.tests.testutils import _msg_noperiodictable, has_periodictable
1112

1213
# ----------------------------------------------------------------------------
1314

1415

1516
class TestAtomRadiiTable(unittest.TestCase):
16-
1717
def setUp(self):
1818
self.rtb = AtomRadiiTable()
1919
self.ctb = ConstantRadiiTable()
@@ -103,9 +103,13 @@ def test_toString(self):
103103
# ----------------------------------------------------------------------------
104104

105105

106-
@unittest.skipUnless(has_periodictable, _msg_noperiodictable)
107106
class TestCovalentRadiiTable(unittest.TestCase):
108107

108+
@pytest.fixture(autouse=True)
109+
def _check_periodictable(self, has_periodictable, _msg_noperiodictable):
110+
if not has_periodictable:
111+
pytest.skip(_msg_noperiodictable)
112+
109113
def setUp(self):
110114
self.rtb = CovalentRadiiTable()
111115
return
File renamed without changes.

Diff for: src/diffpy/srreal/tests/testbondcalculator.py renamed to tests/test_bondcalculator.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
import unittest
88

99
import numpy
10+
import pytest
11+
from testutils import loadDiffPyStructure, loadObjCrystCrystal, pickle_with_attr
1012

1113
from diffpy.srreal.bondcalculator import BondCalculator
12-
from diffpy.srreal.tests.testutils import (
13-
_msg_nopyobjcryst,
14-
has_pyobjcryst,
15-
loadDiffPyStructure,
16-
loadObjCrystCrystal,
17-
pickle_with_attr,
18-
)
1914

2015
# ----------------------------------------------------------------------------
2116

@@ -81,7 +76,7 @@ def test_pickling(self):
8176

8277
def test_pickling_derived_structure(self):
8378
"""Check pickling of BondCalculator with DerivedStructureAdapter."""
84-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
79+
from testutils import DerivedStructureAdapter
8580

8681
bdc = self.bdc
8782
stru0 = DerivedStructureAdapter()
@@ -249,9 +244,13 @@ def test_setTypeMask(self):
249244
# ----------------------------------------------------------------------------
250245

251246

252-
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
253247
class TestBondCalculatorObjCryst(unittest.TestCase):
254248

249+
@pytest.fixture(autouse=True)
250+
def _check_periodictable(self, has_pyobjcryst, _msg_nopyobjcryst):
251+
if not has_pyobjcryst:
252+
pytest.skip(_msg_nopyobjcryst)
253+
255254
def setUp(self):
256255
self.bdc = BondCalculator()
257256
if not hasattr(self, "rutile"):

Diff for: src/diffpy/srreal/tests/testbvscalculator.py renamed to tests/test_bvscalculator.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import pickle
77
import unittest
88

9+
from testutils import loadDiffPyStructure, pickle_with_attr
10+
911
from diffpy.srreal.bvscalculator import BVSCalculator
10-
from diffpy.srreal.tests.testutils import loadDiffPyStructure, pickle_with_attr
1112

1213

1314
##############################################################################
@@ -139,7 +140,7 @@ def test_table_pickling(self):
139140

140141
def test_pickling_derived_structure(self):
141142
"""Check pickling of BVSCalculator with DerivedStructureAdapter."""
142-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
143+
from testutils import DerivedStructureAdapter
143144

144145
bvc = self.bvc
145146
stru0 = DerivedStructureAdapter()

Diff for: src/diffpy/srreal/tests/testdebyepdfcalculator.py renamed to tests/test_debyepdfcalculator.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import unittest
88

99
import numpy
10+
from testutils import _maxNormDiff, loadDiffPyStructure, pickle_with_attr
1011

1112
from diffpy.srreal.pdfcalculator import DebyePDFCalculator, PDFCalculator
12-
from diffpy.srreal.tests.testpdfcalculator import _maxNormDiff
13-
from diffpy.srreal.tests.testutils import loadDiffPyStructure, pickle_with_attr
1413

1514

1615
##############################################################################
@@ -189,7 +188,7 @@ def test_mask_pickling(self):
189188
def test_pickling_derived_structure(self):
190189
"""Check pickling of DebyePDFCalculator with
191190
DerivedStructureAdapter."""
192-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
191+
from testutils import DerivedStructureAdapter
193192

194193
dpdfc = self.dpdfc
195194
stru0 = DerivedStructureAdapter()

0 commit comments

Comments
 (0)