2020import unittest
2121
2222import numpy
23+ import pytest
2324
2425from diffpy .structure import Structure
2526from diffpy .structure .structureerrors import StructureFormatError
26- from diffpy .structure .tests .testutils import datafile
2727
2828# ----------------------------------------------------------------------------
2929
3030
3131class 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