Skip to content

Commit 9f5525e

Browse files
committed
Apply pytest fixtures in unnitest classes
1 parent 40a42f5 commit 9f5525e

File tree

7 files changed

+148
-118
lines changed

7 files changed

+148
-118
lines changed

tests/test_loadstructure.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,57 @@
55

66
import unittest
77

8+
import pytest
9+
810
from diffpy.structure import PDFFitStructure, Structure, loadStructure
911
from diffpy.structure.structureerrors import StructureFormatError
10-
from diffpy.structure.tests.testutils import datafile
1112

1213

1314
##############################################################################
1415
class TestLoadStructure(unittest.TestCase):
16+
@pytest.fixture(autouse=True)
17+
def prepare_fixture(self, datafile):
18+
self.datafile = datafile
1519

1620
def test_xcfg(self):
1721
"""check loading of atomeye xcfg format"""
18-
f = datafile("BubbleRaftShort.xcfg")
22+
f = self.datafile("BubbleRaftShort.xcfg")
1923
stru = loadStructure(f)
2024
self.assertTrue(type(stru) is Structure)
2125
self.assertRaises(StructureFormatError, loadStructure, f, "xyz")
2226
return
2327

2428
def test_discus(self):
2529
"""check loading of discus file format"""
26-
f = datafile("Ni-discus.stru")
30+
f = self.datafile("Ni-discus.stru")
2731
stru = loadStructure(f)
2832
self.assertTrue(type(stru) is PDFFitStructure)
2933
return
3034

3135
def test_cif(self):
3236
"""check loading of CIF file format"""
33-
f = datafile("PbTe.cif")
37+
f = self.datafile("PbTe.cif")
3438
stru = loadStructure(f)
3539
self.assertTrue(isinstance(stru, Structure))
3640
self.assertFalse(isinstance(stru, PDFFitStructure))
3741
return
3842

3943
def test_badfile(self):
4044
"""check loading of CIF file format"""
41-
f = datafile("Ni-bad.stru")
45+
f = self.datafile("Ni-bad.stru")
4246
self.assertRaises(StructureFormatError, loadStructure, f)
4347
return
4448

4549
def test_goodkwarg(self):
4650
"""check loading of CIF file and passing of parser keyword argument."""
47-
f = datafile("graphite.cif")
51+
f = self.datafile("graphite.cif")
4852
stru = loadStructure(f, eps=1e-10)
4953
self.assertEqual(8, len(stru))
5054
return
5155

5256
def test_badkwarg(self):
5357
"""check loading of xyz file format with invalid keyword argument"""
54-
f = datafile("bucky.xyz")
58+
f = self.datafile("bucky.xyz")
5559
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
5660
return
5761

tests/test_p_cif.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import unittest
2020

2121
import numpy
22+
import pytest
2223

2324
from diffpy.structure import Structure
2425
from diffpy.structure.parsers import getParser
2526
from diffpy.structure.parsers.p_cif import P_cif, getSymOp, leading_float
2627
from diffpy.structure.structureerrors import StructureFormatError
27-
from diffpy.structure.tests.testutils import datafile
2828

2929
# ----------------------------------------------------------------------------
3030

@@ -65,14 +65,16 @@ def test_getSymOp(self):
6565

6666

6767
class TestP_cif(unittest.TestCase):
68-
69-
pbteciffile = datafile("PbTe.cif")
70-
badciffile = datafile("LiCl-bad.cif")
71-
graphiteciffile = datafile("graphite.cif")
72-
cdsebulkpdffitfile = datafile("CdSe_bulk.stru")
73-
teiciffile = datafile("TeI.cif")
74-
refciffile = datafile("Ni_ref.cif")
75-
places = 6
68+
@pytest.fixture(autouse=True)
69+
def prepare_fixture(self, datafile):
70+
self.datafile = datafile
71+
self.pbteciffile = datafile("PbTe.cif")
72+
self.badciffile = datafile("LiCl-bad.cif")
73+
self.graphiteciffile = datafile("graphite.cif")
74+
self.cdsebulkpdffitfile = datafile("CdSe_bulk.stru")
75+
self.teiciffile = datafile("TeI.cif")
76+
self.refciffile = datafile("Ni_ref.cif")
77+
self.places = 6
7678

7779
def setUp(self):
7880
self.ptest = P_cif()
@@ -240,7 +242,7 @@ def test_eps(self):
240242

241243
def test_unknown_occupancy(self):
242244
"test CIF file with unknown occupancy data"
243-
stru = self.ptest.parseFile(datafile("TeI-unkocc.cif"))
245+
stru = self.ptest.parseFile(self.datafile("TeI-unkocc.cif"))
244246
self.assertTrue(numpy.array_equal(16 * [1], stru.occupancy))
245247
return
246248

@@ -268,7 +270,7 @@ def test_unknown_spacegroup_number(self):
268270
def test_nosites_cif(self):
269271
"""Test reading of CIF file with no valid sites."""
270272
ptest = self.ptest
271-
stru = ptest.parseFile(datafile("nosites.cif"))
273+
stru = ptest.parseFile(self.datafile("nosites.cif"))
272274
self.assertEqual(0, len(stru))
273275
self.assertEqual(10.413, stru.lattice.a)
274276
self.assertEqual(10.413, stru.lattice.b)
@@ -278,14 +280,14 @@ def test_nosites_cif(self):
278280
def test_badspacegroup_cif(self):
279281
"""Test reading of CIF file with unrecognized space group."""
280282
ptest = self.ptest
281-
filename = datafile("badspacegroup.cif")
283+
filename = self.datafile("badspacegroup.cif")
282284
self.assertRaises(StructureFormatError, ptest.parseFile, filename)
283285
return
284286

285287
def test_custom_spacegroup_cif(self):
286288
"""Test parsing of nonstandard symops-defined space group."""
287289
pfile = self.pfile
288-
filename = datafile("customsg.cif")
290+
filename = self.datafile("customsg.cif")
289291
pfile.parseFile(filename)
290292
sg = pfile.spacegroup
291293
self.assertEqual("CIF data", sg.short_name)
@@ -363,7 +365,7 @@ def test_unknown_aniso(self):
363365
def test_curly_brace(self):
364366
"verify loading of a CIF file with unquoted curly brace"
365367
ptest = self.ptest
366-
stru = ptest.parseFile(datafile("curlybrackets.cif"))
368+
stru = ptest.parseFile(self.datafile("curlybrackets.cif"))
367369
self.assertEqual(20, len(stru))
368370
return
369371

tests/test_p_discus.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
import re
2020
import unittest
2121

22+
import pytest
23+
2224
from diffpy.structure import Structure
2325
from diffpy.structure.parsers import StructureFormatError
24-
from diffpy.structure.tests.testutils import datafile
2526

2627
# ----------------------------------------------------------------------------
2728

2829

2930
class TestP_discus(unittest.TestCase):
3031
"""test Parser for PDFFit file format"""
3132

33+
@pytest.fixture(autouse=True)
34+
def prepare_fixture(self, datafile):
35+
self.datafile = datafile
36+
3237
def setUp(self):
3338
self.stru = Structure()
3439
self.format = "discus"
@@ -37,7 +42,7 @@ def setUp(self):
3742
def test_read_discus_Ni(self):
3843
"""check reading of Ni structure in discus format"""
3944
stru = self.stru
40-
stru.read(datafile("Ni-discus.stru"), self.format)
45+
stru.read(self.datafile("Ni-discus.stru"), self.format)
4146
f_title = "structure Ni FCC"
4247
self.assertEqual(f_title, stru.title)
4348
self.assertEqual("Fm-3m", stru.pdffit["spcgr"])
@@ -75,15 +80,15 @@ def test_except_other_formats(self):
7580
"hexagon-raw.xyz",
7681
]
7782
for ft in badfiles:
78-
ff = datafile(ft)
83+
ff = self.datafile(ft)
7984
self.assertRaises(StructureFormatError, self.stru.read, ff, format=self.format)
8085
return
8186

8287
def test_ignored_lines(self):
8388
"""check skipping of ignored lines in the header"""
8489
r1 = "ignored record 1\n"
8590
r2 = "ignored record 2\n"
86-
with open(datafile("Ni-discus.stru")) as fp:
91+
with open(self.datafile("Ni-discus.stru")) as fp:
8792
ni_lines = fp.readlines()
8893
ni_lines.insert(2, r1)
8994
ni_lines.insert(4, r2)
@@ -98,7 +103,7 @@ def test_ignored_lines(self):
98103
def test_spdiameter_parsing(self):
99104
"""check parsing of spdiameter record from a file."""
100105
stru = self.stru
101-
stru.read(datafile("Ni-discus.stru"), self.format)
106+
stru.read(self.datafile("Ni-discus.stru"), self.format)
102107
self.assertEqual(0, stru.pdffit["spdiameter"])
103108
snoshape = stru.writeStr(format=self.format)
104109
self.assertTrue(not re.search("(?m)^shape", snoshape))
@@ -109,7 +114,7 @@ def test_spdiameter_parsing(self):
109114
stru13 = Structure()
110115
stru13.readStr(s13)
111116
self.assertEqual(13, stru13.pdffit["spdiameter"])
112-
with open(datafile("Ni.stru")) as fp:
117+
with open(self.datafile("Ni.stru")) as fp:
113118
ni_lines = fp.readlines()
114119
ni_lines.insert(3, "shape invalid, 7\n")
115120
sbad = "".join(ni_lines)
@@ -119,7 +124,7 @@ def test_spdiameter_parsing(self):
119124
def test_stepcut_parsing(self):
120125
"""check parsing of stepcut record from a file."""
121126
stru = self.stru
122-
stru.read(datafile("Ni-discus.stru"), self.format)
127+
stru.read(self.datafile("Ni-discus.stru"), self.format)
123128
self.assertEqual(0, stru.pdffit["stepcut"])
124129
snoshape = stru.writeStr(format=self.format)
125130
self.assertTrue(not re.search("(?m)^shape", snoshape))
@@ -130,7 +135,7 @@ def test_stepcut_parsing(self):
130135
stru13 = Structure()
131136
stru13.readStr(s13)
132137
self.assertEqual(13, stru13.pdffit["stepcut"])
133-
with open(datafile("Ni.stru")) as fp:
138+
with open(self.datafile("Ni.stru")) as fp:
134139
ni_lines = fp.readlines()
135140
ni_lines.insert(3, "shape invalid, 7\n")
136141
sbad = "".join(ni_lines)

tests/test_p_pdffit.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
import unittest
2121

2222
import numpy
23+
import pytest
2324

2425
from diffpy.structure import Structure
2526
from diffpy.structure.structureerrors import StructureFormatError
26-
from diffpy.structure.tests.testutils import datafile
2727

2828
# ----------------------------------------------------------------------------
2929

3030

3131
class TestP_pdffit(unittest.TestCase):
3232
"""test Parser for PDFFit file format"""
3333

34+
@pytest.fixture(autouse=True)
35+
def prepare_fixture(self, datafile):
36+
self.datafile = datafile
37+
3438
def setUp(self):
3539
self.stru = Structure()
3640
self.format = "pdffit"
@@ -39,7 +43,7 @@ def setUp(self):
3943
def test_read_pdffit_ZnSb(self):
4044
"""check reading of ZnSb pdffit structure file"""
4145
stru = self.stru
42-
stru.read(datafile("ZnSb_RT_Q28X_VM_20_fxiso.rstr"), self.format)
46+
stru.read(self.datafile("ZnSb_RT_Q28X_VM_20_fxiso.rstr"), self.format)
4347
f_title = "Cell structure file of Zn4Sb3 #167 interstitial"
4448
self.assertEqual(stru.title, f_title)
4549
self.assertAlmostEqual(stru.pdffit["scale"], 0.826145)
@@ -84,7 +88,7 @@ def test_read_pdffit_ZnSb(self):
8488
def test_read_pdffit_Ni(self):
8589
"""check reading of Ni pdffit structure file"""
8690
stru = self.stru
87-
stru.read(datafile("Ni.stru"), self.format)
91+
stru.read(self.datafile("Ni.stru"), self.format)
8892
f_title = "structure Ni FCC"
8993
self.assertEqual(stru.title, f_title)
9094
self.assertEqual(stru.pdffit["spcgr"], "Fm-3m")
@@ -117,7 +121,7 @@ def test_read_pdffit_Ni(self):
117121
def test_read_pdffit_Ni_prim123(self):
118122
"""check reading of Ni_prim supercell 1x2x3"""
119123
stru = self.stru
120-
stru.read(datafile("Ni_prim123.stru"), self.format)
124+
stru.read(self.datafile("Ni_prim123.stru"), self.format)
121125
s_lat = [
122126
stru.lattice.a,
123127
stru.lattice.b,
@@ -148,15 +152,15 @@ def test_read_pdffit_Ni_prim123(self):
148152
def test_read_pdffit_bad(self):
149153
"""check exceptions when reading invalid pdffit file"""
150154
stru = self.stru
151-
self.assertRaises(StructureFormatError, stru.read, datafile("Ni-bad.stru"), self.format)
152-
self.assertRaises(StructureFormatError, stru.read, datafile("bucky.xyz"), self.format)
155+
self.assertRaises(StructureFormatError, stru.read, self.datafile("Ni-bad.stru"), self.format)
156+
self.assertRaises(StructureFormatError, stru.read, self.datafile("bucky.xyz"), self.format)
153157
return
154158

155159
def test_writeStr_pdffit(self):
156160
"""check writing of normal xyz file"""
157161
stru = self.stru
158-
stru.read(datafile("Ni.stru"), self.format)
159-
with open(datafile("Ni.stru")) as fp:
162+
stru.read(self.datafile("Ni.stru"), self.format)
163+
with open(self.datafile("Ni.stru")) as fp:
160164
f_s = fp.read()
161165
f_s = re.sub("[ \t]+", " ", f_s)
162166
f_s = re.sub("[ \t]+\n", "\n", f_s)
@@ -167,7 +171,7 @@ def test_writeStr_pdffit(self):
167171

168172
def test_huge_occupancy(self):
169173
"""check structure with huge occupancy can be read."""
170-
self.stru.read(datafile("Ni.stru"), self.format)
174+
self.stru.read(self.datafile("Ni.stru"), self.format)
171175
self.stru[0].occupancy = 16e16
172176
s_s = self.stru.writeStr(self.format)
173177
stru1 = Structure()
@@ -179,7 +183,7 @@ def test_ignored_lines(self):
179183
"""check skipping of ignored lines in the header"""
180184
r1 = "ignored record 1"
181185
r2 = "ignored record 2"
182-
with open(datafile("Ni.stru")) as fp:
186+
with open(self.datafile("Ni.stru")) as fp:
183187
ni_lines = fp.readlines()
184188
ni_lines.insert(2, r1 + "\n")
185189
ni_lines.insert(4, r2 + "\n")
@@ -194,7 +198,7 @@ def test_ignored_lines(self):
194198
def test_spdiameter_parsing(self):
195199
"""check parsing of spdiameter record from a file."""
196200
stru = self.stru
197-
stru.read(datafile("Ni.stru"), self.format)
201+
stru.read(self.datafile("Ni.stru"), self.format)
198202
self.assertEqual(0, stru.pdffit["spdiameter"])
199203
snoshape = stru.writeStr(format=self.format)
200204
self.assertTrue(not re.search("(?m)^shape", snoshape))
@@ -205,7 +209,7 @@ def test_spdiameter_parsing(self):
205209
stru13 = Structure()
206210
stru13.readStr(s13)
207211
self.assertEqual(13, stru13.pdffit["spdiameter"])
208-
with open(datafile("Ni.stru")) as fp:
212+
with open(self.datafile("Ni.stru")) as fp:
209213
ni_lines = fp.readlines()
210214
ni_lines.insert(3, "shape invalid, 7\n")
211215
sbad = "".join(ni_lines)
@@ -215,7 +219,7 @@ def test_spdiameter_parsing(self):
215219
def test_stepcut_parsing(self):
216220
"""check parsing of stepcut record from a file."""
217221
stru = self.stru
218-
stru.read(datafile("Ni.stru"), self.format)
222+
stru.read(self.datafile("Ni.stru"), self.format)
219223
self.assertEqual(0, stru.pdffit["stepcut"])
220224
snoshape = stru.writeStr(format=self.format)
221225
self.assertTrue(not re.search("(?m)^shape", snoshape))
@@ -226,7 +230,7 @@ def test_stepcut_parsing(self):
226230
stru13 = Structure()
227231
stru13.readStr(s13)
228232
self.assertEqual(13, stru13.pdffit["stepcut"])
229-
with open(datafile("Ni.stru")) as fp:
233+
with open(self.datafile("Ni.stru")) as fp:
230234
ni_lines = fp.readlines()
231235
ni_lines.insert(3, "shape invalid, 7\n")
232236
sbad = "".join(ni_lines)

0 commit comments

Comments
 (0)