Skip to content

Commit 70e88e4

Browse files
committed
fix pytest
1 parent f01987a commit 70e88e4

18 files changed

Lines changed: 136 additions & 110 deletions

tests/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import json
2+
import logging
23
from pathlib import Path
34

45
import pytest
56

7+
from diffpy.srreal.structureconverters import convertObjCrystCrystal
8+
import diffpy.structure as mod_structure
9+
610

711
@pytest.fixture
812
def user_filesystem(tmp_path):
@@ -17,3 +21,55 @@ def user_filesystem(tmp_path):
1721
json.dump(home_config_data, f)
1822

1923
yield tmp_path
24+
25+
26+
# Resolve availability of optional packages.
27+
28+
# pyobjcryst
29+
30+
31+
@pytest.fixture(scope="session")
32+
def _msg_nopyobjcryst():
33+
return "No module named 'pyobjcryst'"
34+
35+
36+
@pytest.fixture(scope="session")
37+
def has_pyobjcryst():
38+
try:
39+
import pyobjcryst.crystal
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
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import pickle
77
import unittest
8+
import pytest
89

910
from diffpy.srreal.atomradiitable import AtomRadiiTable, ConstantRadiiTable, CovalentRadiiTable
10-
from diffpy.srreal.tests.testutils import _msg_noperiodictable, has_periodictable
1111

1212
# ----------------------------------------------------------------------------
1313

14-
1514
class TestAtomRadiiTable(unittest.TestCase):
16-
1715
def setUp(self):
1816
self.rtb = AtomRadiiTable()
1917
self.ctb = ConstantRadiiTable()
@@ -102,10 +100,13 @@ def test_toString(self):
102100

103101
# ----------------------------------------------------------------------------
104102

105-
106-
@unittest.skipUnless(has_periodictable, _msg_noperiodictable)
107103
class TestCovalentRadiiTable(unittest.TestCase):
108104

105+
@pytest.fixture(autouse=True)
106+
def _check_periodictable(self, has_periodictable, _msg_noperiodictable):
107+
if not has_periodictable:
108+
pytest.skip(_msg_noperiodictable)
109+
109110
def setUp(self):
110111
self.rtb = CovalentRadiiTable()
111112
return
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
import pickle
77
import unittest
8+
import pytest
89

910
import numpy
1011

1112
from diffpy.srreal.bondcalculator import BondCalculator
12-
from diffpy.srreal.tests.testutils import (
13-
_msg_nopyobjcryst,
14-
has_pyobjcryst,
13+
from testutils import (
1514
loadDiffPyStructure,
1615
loadObjCrystCrystal,
1716
pickle_with_attr,
@@ -81,7 +80,7 @@ def test_pickling(self):
8180

8281
def test_pickling_derived_structure(self):
8382
"""Check pickling of BondCalculator with DerivedStructureAdapter."""
84-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
83+
from testutils import DerivedStructureAdapter
8584

8685
bdc = self.bdc
8786
stru0 = DerivedStructureAdapter()
@@ -248,10 +247,13 @@ def test_setTypeMask(self):
248247

249248
# ----------------------------------------------------------------------------
250249

251-
252-
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
253250
class TestBondCalculatorObjCryst(unittest.TestCase):
254251

252+
@pytest.fixture(autouse=True)
253+
def _check_periodictable(self, has_pyobjcryst, _msg_nopyobjcryst):
254+
if not has_pyobjcryst:
255+
pytest.skip(_msg_nopyobjcryst)
256+
255257
def setUp(self):
256258
self.bdc = BondCalculator()
257259
if not hasattr(self, "rutile"):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import unittest
88

99
from diffpy.srreal.bvscalculator import BVSCalculator
10-
from diffpy.srreal.tests.testutils import loadDiffPyStructure, pickle_with_attr
10+
from testutils import loadDiffPyStructure, pickle_with_attr
1111

1212

1313
##############################################################################
@@ -139,7 +139,7 @@ def test_table_pickling(self):
139139

140140
def test_pickling_derived_structure(self):
141141
"""Check pickling of BVSCalculator with DerivedStructureAdapter."""
142-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
142+
from testutils import DerivedStructureAdapter
143143

144144
bvc = self.bvc
145145
stru0 = DerivedStructureAdapter()
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import numpy
1010

1111
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
12+
from testutils import loadDiffPyStructure, pickle_with_attr, _maxNormDiff
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()
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import copy
77
import pickle
88
import unittest
9+
import pytest
910

1011
import numpy
1112

1213
from diffpy.srreal.atomradiitable import CovalentRadiiTable
1314
from diffpy.srreal.overlapcalculator import OverlapCalculator
14-
from diffpy.srreal.tests.testutils import (
15-
_msg_nopyobjcryst,
16-
has_pyobjcryst,
15+
from testutils import (
1716
loadDiffPyStructure,
1817
loadObjCrystCrystal,
1918
pickle_with_attr,
@@ -117,7 +116,7 @@ def test_pickling_artb(self):
117116

118117
def test_pickling_derived_structure(self):
119118
"""Check pickling of OverlapCalculator with DerivedStructureAdapter."""
120-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
119+
from testutils import DerivedStructureAdapter
121120

122121
olc = self.olc
123122
stru0 = DerivedStructureAdapter()
@@ -331,10 +330,13 @@ def test_neighborhoods(self):
331330

332331
# ----------------------------------------------------------------------------
333332

334-
335-
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
336333
class TestOverlapCalculatorObjCryst(unittest.TestCase):
337334

335+
@pytest.fixture(autouse=True)
336+
def _check_periodictable(self, has_pyobjcryst, _msg_nopyobjcryst):
337+
if not has_pyobjcryst:
338+
pytest.skip(_msg_nopyobjcryst)
339+
338340
def setUp(self):
339341
self.olc = OverlapCalculator()
340342
if not hasattr(self, "rutile"):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from diffpy.srreal.pairquantity import PairQuantity
1111
from diffpy.srreal.pdfcalculator import PDFCalculator
1212
from diffpy.srreal.srreal_ext import BasePairQuantity
13-
from diffpy.srreal.tests.testutils import mod_structure
13+
from testutils import mod_structure
1414

1515
# ----------------------------------------------------------------------------
1616

@@ -169,7 +169,7 @@ def eval_as(evtp, pq, stru):
169169

170170
def test_pickling(self):
171171
"""Check pickling and unpickling of PairQuantity."""
172-
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
172+
from testutils import DerivedStructureAdapter
173173

174174
stru0 = DerivedStructureAdapter()
175175
self.pq.setStructure(stru0)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy
1010

1111
from diffpy.srreal.parallel import createParallelCalculator
12-
from diffpy.srreal.tests.testutils import loadDiffPyStructure
12+
from testutils import loadDiffPyStructure
1313

1414

1515
##############################################################################
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from diffpy.srreal.pdfbaseline import LinearBaseline, PDFBaseline, ZeroBaseline, makePDFBaseline
1212
from diffpy.srreal.pdfcalculator import PDFCalculator
13-
from diffpy.srreal.tests.testutils import pickle_with_attr
13+
from testutils import pickle_with_attr
1414

1515
# ----------------------------------------------------------------------------
1616

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import re
77
import unittest
8+
import pytest
89

910
import numpy
1011

1112
from diffpy.srreal.pdfcalculator import PDFCalculator
12-
from diffpy.srreal.tests.testpdfcalculator import _maxNormDiff
13-
from diffpy.srreal.tests.testutils import _msg_nopyobjcryst, datafile, has_pyobjcryst, loadObjCrystCrystal
13+
from testutils import datafile, loadObjCrystCrystal, _maxNormDiff
1414

1515
# helper functions
1616

@@ -58,10 +58,13 @@ def _makePDFCalculator(crst, cfgdict):
5858

5959
# ----------------------------------------------------------------------------
6060

61-
62-
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
6361
class TestPDFCalcObjcryst(unittest.TestCase):
6462

63+
@pytest.fixture(autouse=True)
64+
def _check_periodictable(self, has_pyobjcryst, _msg_nopyobjcryst):
65+
if not has_pyobjcryst:
66+
pytest.skip(_msg_nopyobjcryst)
67+
6568
def _comparePDFs(self, nickname, pdfbasename, cifbasename):
6669
def setself(**kwtoset):
6770
for n, v in kwtoset.items():

0 commit comments

Comments
 (0)