Skip to content

Commit c19f1b7

Browse files
committed
fix pytest fixture for 3.11
pcmt on tests
1 parent be15d50 commit c19f1b7

11 files changed

+100
-60
lines changed

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from pyobjcryst.crystal import create_crystal_from_cif
7+
68

79
@pytest.fixture
810
def user_filesystem(tmp_path):
@@ -17,3 +19,23 @@ def user_filesystem(tmp_path):
1719
json.dump(home_config_data, f)
1820

1921
yield tmp_path
22+
23+
24+
@pytest.fixture
25+
def datafile():
26+
"""Fixture to dynamically load any test file."""
27+
28+
def _load(filename):
29+
return "tests/testdata/" + filename
30+
31+
return _load
32+
33+
34+
@pytest.fixture
35+
def loadcifdata():
36+
"""Fixture to load CIF data files for testing."""
37+
38+
def _load(filename):
39+
return create_crystal_from_cif("tests/testdata/" + filename)
40+
41+
return _load

tests/test_cif.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import gc
1818
import unittest
1919

20+
import pytest
2021
from numpy import pi
21-
from utils import datafile, loadcifdata
2222

2323
from pyobjcryst.crystal import CreateCrystalFromCIF
2424
from pyobjcryst.diffractiondatasinglecrystal import (
@@ -29,33 +29,38 @@
2929

3030
class TestCif(unittest.TestCase):
3131

32+
@pytest.fixture(autouse=True)
33+
def prepare_fixture(self, loadcifdata, datafile):
34+
self.loadcifdata = loadcifdata
35+
self.datafile = datafile
36+
3237
def test_Ag_silver_cif(self):
3338
"""Check loading of Ag_silver.cif."""
34-
c = loadcifdata("Ag_silver.cif")
39+
c = self.loadcifdata("Ag_silver.cif")
3540
self.assertTrue(c is not None)
3641
return
3742

3843
def test_BaTiO3_cif(self):
3944
"""Check loading of BaTiO3.cif."""
40-
c = loadcifdata("BaTiO3.cif")
45+
c = self.loadcifdata("BaTiO3.cif")
4146
self.assertTrue(c is not None)
4247
return
4348

4449
def test_C_graphite_hex_cif(self):
4550
"""Check loading of C_graphite_hex.cif."""
46-
c = loadcifdata("C_graphite_hex.cif")
51+
c = self.loadcifdata("C_graphite_hex.cif")
4752
self.assertTrue(c is not None)
4853
return
4954

5055
def test_CaF2_fluorite_cif(self):
5156
"""Check loading of CaF2_fluorite.cif."""
52-
c = loadcifdata("CaF2_fluorite.cif")
57+
c = self.loadcifdata("CaF2_fluorite.cif")
5358
self.assertTrue(c is not None)
5459
return
5560

5661
def test_caffeine_cif(self):
5762
"""Check loading of caffeine.cif and the data inside."""
58-
c = loadcifdata("caffeine.cif")
63+
c = self.loadcifdata("caffeine.cif")
5964
self.assertTrue(c is not None)
6065
self.assertEqual(24, c.GetNbScatterer())
6166
self.assertAlmostEqual(14.9372, c.a, 6)
@@ -103,7 +108,7 @@ def test_caffeine_cif(self):
103108

104109
def test_CaTiO3_cif(self):
105110
"""Check loading of CaTiO3.cif and its ADPs."""
106-
c = loadcifdata("CaTiO3.cif")
111+
c = self.loadcifdata("CaTiO3.cif")
107112
self.assertTrue(c is not None)
108113
s = c.GetScatt(3)
109114
name = s.GetName()
@@ -125,79 +130,79 @@ def test_CaTiO3_cif(self):
125130

126131
def test_CdSe_cadmoselite_cif(self):
127132
"""Check loading of CdSe_cadmoselite.cif."""
128-
c = loadcifdata("CdSe_cadmoselite.cif")
133+
c = self.loadcifdata("CdSe_cadmoselite.cif")
129134
self.assertTrue(c is not None)
130135
return
131136

132137
def test_CeO2_cif(self):
133138
"""Check loading of CeO2.cif."""
134-
c = loadcifdata("CeO2.cif")
139+
c = self.loadcifdata("CeO2.cif")
135140
self.assertTrue(c is not None)
136141
return
137142

138143
def test_lidocainementhol_cif(self):
139144
"""Check loading of lidocainementhol.cif."""
140-
c = loadcifdata("lidocainementhol.cif")
145+
c = self.loadcifdata("lidocainementhol.cif")
141146
self.assertTrue(c is not None)
142147
return
143148

144149
def test_NaCl_cif(self):
145150
"""Check loading of NaCl.cif."""
146-
c = loadcifdata("NaCl.cif")
151+
c = self.loadcifdata("NaCl.cif")
147152
self.assertTrue(c is not None)
148153
return
149154

150155
def test_Ni_cif(self):
151156
"""Check loading of Ni.cif."""
152-
c = loadcifdata("Ni.cif")
157+
c = self.loadcifdata("Ni.cif")
153158
self.assertTrue(c is not None)
154159
return
155160

156161
def test_paracetamol_cif(self):
157162
"""Check loading of paracetamol.cif."""
158-
c = loadcifdata("paracetamol.cif")
163+
c = self.loadcifdata("paracetamol.cif")
159164
self.assertTrue(c is not None)
160165
return
161166

162167
def test_PbS_galena_cif(self):
163168
"""Check loading of PbS_galena.cif."""
164-
c = loadcifdata("PbS_galena.cif")
169+
c = self.loadcifdata("PbS_galena.cif")
165170
self.assertTrue(c is not None)
166171
return
167172

168173
def test_PbTe_cif(self):
169174
"""Check loading of PbTe.cif."""
170-
c = loadcifdata("PbTe.cif")
175+
c = self.loadcifdata("PbTe.cif")
171176
self.assertTrue(c is not None)
172177
return
173178

174179
def test_Si_cif(self):
175180
"""Check loading of Si.cif."""
176-
c = loadcifdata("Si.cif")
181+
c = self.loadcifdata("Si.cif")
177182
self.assertTrue(c is not None)
178183
return
179184

180185
def test_Si_setting2_cif(self):
181186
"""Check loading of Si_setting2.cif."""
182-
c = loadcifdata("Si_setting2.cif")
187+
c = self.loadcifdata("Si_setting2.cif")
183188
self.assertTrue(c is not None)
184189
return
185190

186191
def test_SrTiO3_tausonite_cif(self):
187192
"""Check loading of SrTiO3_tausonite.cif."""
188-
c = loadcifdata("SrTiO3_tausonite.cif")
193+
c = self.loadcifdata("SrTiO3_tausonite.cif")
189194
self.assertTrue(c is not None)
190195
return
191196

192197
def test_TiO2_anatase_cif(self):
193198
"""Check loading of TiO2_anatase.cif."""
194-
c = loadcifdata("TiO2_anatase.cif")
199+
c = self.loadcifdata("TiO2_anatase.cif")
195200
self.assertTrue(c is not None)
196201
return
197202

198203
def test_TiO2_rutile_cif(self):
199204
"""Check loading of TiO2_rutile.cif and its ADP data."""
200-
c = loadcifdata("TiO2_rutile.cif")
205+
c = self.loadcifdata("TiO2_rutile.cif")
201206
self.assertTrue(c is not None)
202207
s = c.GetScatt(0)
203208
name = s.GetName()
@@ -210,50 +215,56 @@ def test_TiO2_rutile_cif(self):
210215

211216
def test_Zn_zinc_cif(self):
212217
"""Check loading of Zn_zinc.cif."""
213-
c = loadcifdata("Zn_zinc.cif")
218+
c = self.loadcifdata("Zn_zinc.cif")
214219
self.assertTrue(c is not None)
215220
return
216221

217222
def test_ZnS_sphalerite_cif(self):
218223
"""Check loading of ZnS_sphalerite.cif."""
219-
c = loadcifdata("ZnS_sphalerite.cif")
224+
c = self.loadcifdata("ZnS_sphalerite.cif")
220225
self.assertTrue(c is not None)
221226
return
222227

223228
def test_ZnS_wurtzite_cif(self):
224229
"""Check loading of ZnS_wurtzite.cif."""
225-
c = loadcifdata("ZnS_wurtzite.cif")
230+
c = self.loadcifdata("ZnS_wurtzite.cif")
226231
self.assertTrue(c is not None)
227232
return
228233

229234
def testBadCif(self):
230235
"""Make sure we can read all cif files."""
231236
from pyobjcryst import ObjCrystException
232237

233-
fname = datafile("ni.stru")
238+
fname = self.datafile("ni.stru")
234239
infile = open(fname, "rb")
235240
self.assertRaises(ObjCrystException, CreateCrystalFromCIF, infile)
236241
infile.close()
237242
return
238243

239244
def test_paracetamol_monomethanolate(self):
240245
"""Test loading crystal and diffraction data."""
241-
c = loadcifdata("paracetamol_monomethanolate.cif")
246+
c = self.loadcifdata("paracetamol_monomethanolate.cif")
242247
d = create_singlecrystaldata_from_cif(
243-
datafile("paracetamol_monomethanolate_data_single_crystal.cif"), c
248+
self.datafile(
249+
"paracetamol_monomethanolate_data_single_crystal.cif"
250+
),
251+
c,
244252
)
245253
self.assertTrue(d is not None)
246254

247255
def test_paracetamol_monomethanolate_ward(self):
248256
"""Test loading crystal and diffraction data, make sure
249257
custodian & ward works."""
250-
c = loadcifdata("paracetamol_monomethanolate.cif")
258+
c = self.loadcifdata("paracetamol_monomethanolate.cif")
251259
d = create_singlecrystaldata_from_cif(
252-
datafile("paracetamol_monomethanolate_data_single_crystal.cif"), c
260+
self.datafile(
261+
"paracetamol_monomethanolate_data_single_crystal.cif"
262+
),
263+
c,
253264
)
254265
n = d.GetCrystal().GetName()
255266
# Replace c by another Crystal object
256-
c = loadcifdata("ZnS_sphalerite.cif")
267+
c = self.loadcifdata("ZnS_sphalerite.cif")
257268
gc.collect()
258269
self.assertTrue(gCrystalRegistry.GetObj(n) is not None)
259270

tests/test_clocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import unittest
1818

19-
from utils import makeC60
19+
from testutils import makeC60
2020

2121
from pyobjcryst.refinableobj import RefinableObjClock
2222

tests/test_crystal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import unittest
1818

19-
from utils import (
19+
from testutils import (
2020
getScatterer,
2121
makeCrystal,
2222
makeScatterer,

tests/test_globaloptim.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import unittest
1313

14-
from utils import loadcifdata
14+
import pytest
1515

1616
from pyobjcryst import refinableobj
1717
from pyobjcryst.diffractiondatasinglecrystal import (
@@ -22,8 +22,12 @@
2222

2323
class TestGlobalOptim(unittest.TestCase):
2424

25+
@pytest.fixture(autouse=True)
26+
def prepare_fixture(self, loadcifdata):
27+
self.loadcifdata = loadcifdata
28+
2529
def setUp(self):
26-
self.c = loadcifdata("caffeine.cif")
30+
self.c = self.loadcifdata("caffeine.cif")
2731
self.d = DiffractionDataSingleCrystal(self.c)
2832
self.d.GenHKLFullSpace2(0.4, True)
2933
self.d.SetIobsToIcalc()

tests/test_lsq.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import unittest
1313

14-
from utils import loadcifdata
14+
import pytest
1515

1616
from pyobjcryst import refinableobj
1717
from pyobjcryst.diffractiondatasinglecrystal import (
@@ -22,8 +22,12 @@
2222

2323
class TestGlobalOptim(unittest.TestCase):
2424

25+
@pytest.fixture(autouse=True)
26+
def prepare_fixture(self, loadcifdata):
27+
self.loadcifdata = loadcifdata
28+
2529
def setUp(self):
26-
self.c = loadcifdata("caffeine.cif")
30+
self.c = self.loadcifdata("caffeine.cif")
2731
self.d = DiffractionDataSingleCrystal(self.c)
2832
self.d.GenHKLFullSpace2(0.4, True)
2933
self.d.SetIobsToIcalc()

tests/test_molecule.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
"""Tests for molecule module."""
1616

1717
import unittest
18-
from importlib.resources import files
1918

19+
import pytest
2020
from numpy import pi
21-
from utils import makeC60, makeMnO6
21+
from testutils import makeC60, makeMnO6
2222

2323
from pyobjcryst import ObjCrystException
2424
from pyobjcryst.crystal import Crystal
@@ -38,6 +38,10 @@
3838

3939
class TestMolecule(unittest.TestCase):
4040

41+
@pytest.fixture(autouse=True)
42+
def prepare_fixture(self, datafile):
43+
self.datafile = datafile
44+
4145
def setUp(self):
4246
self.c = makeC60()
4347
self.m = self.c.GetScatterer("c60")
@@ -445,7 +449,7 @@ def testManipulation(self):
445449

446450
def testZMatrix(self):
447451
"""Test creating a Molecule from a z-matrix."""
448-
fname = str(files(__name__).joinpath("testdata", "cime.fhz"))
452+
fname = self.datafile("cime.fhz")
449453
c = Crystal()
450454
m = ImportFenskeHallZMatrix(c, fname)
451455
assert m.GetNbAtoms() == 17

0 commit comments

Comments
 (0)