Skip to content

Commit 96e86ed

Browse files
committed
Replace most pytest xfails with raises(Error), in line with best practice
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
1 parent 9d869e3 commit 96e86ed

13 files changed

+82
-87
lines changed

diffsims/tests/generators/test_diffraction_generator.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -248,36 +248,36 @@ def test_defaults(self, diffraction_calculator_atomic, local_structure):
248248

249249
np.testing.assert_allclose(diffraction1, diffraction2, 1e-6, 1e-6)
250250

251-
@pytest.mark.xfail(raises=NotImplementedError)
252251
def test_mode(self, diffraction_calculator_atomic, local_structure):
253-
diffraction = diffraction_calculator_atomic.calculate_ed_data(
254-
local_structure, probe, 1, mode="other"
255-
)
252+
with pytest.raises(NotImplementedError):
253+
_ = diffraction_calculator_atomic.calculate_ed_data(
254+
local_structure, probe, 1, mode="other"
255+
)
256256

257-
@pytest.mark.xfail(raises=ValueError, strict=True)
258257
def test_bad_ZERO(self, diffraction_calculator_atomic, local_structure):
259-
_ = diffraction_calculator_atomic.calculate_ed_data(
260-
local_structure, probe, 1, ZERO=-1
261-
)
258+
with pytest.raises(ValueError):
259+
_ = diffraction_calculator_atomic.calculate_ed_data(
260+
local_structure, probe, 1, ZERO=-1
261+
)
262262

263263

264264
@pytest.mark.parametrize("scattering_param", ["lobato", "xtables"])
265265
def test_param_check(scattering_param):
266266
generator = DiffractionGenerator(300, scattering_params=scattering_param)
267267

268268

269-
@pytest.mark.xfail(raises=NotImplementedError)
270269
def test_invalid_scattering_params():
271270
scattering_param = "_empty"
272-
generator = DiffractionGenerator(300, scattering_params=scattering_param)
271+
with pytest.raises(NotImplementedError):
272+
_ = DiffractionGenerator(300, scattering_params=scattering_param)
273273

274274

275-
@pytest.mark.xfail(faises=NotImplementedError)
276275
def test_invalid_shape_model():
277-
generator = DiffractionGenerator(300, shape_factor_model="dracula")
276+
with pytest.raises(NotImplementedError):
277+
_ = DiffractionGenerator(300, shape_factor_model="dracula")
278278

279279

280280
@pytest.mark.parametrize("shape", [(10, 20), (20, 10)])
281281
def test_param_check_atomic(shape):
282282
detector = [np.linspace(-1, 1, s) for s in shape]
283-
generator = AtomicDiffractionGenerator(300, detector, True)
283+
_ = AtomicDiffractionGenerator(300, detector, True)

diffsims/tests/generators/test_rotation_list_generator.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ def test_get_beam_directions_grid_size(crystal_system, desired_size):
9494
assert grid.shape[0] == desired_size
9595

9696

97-
@pytest.mark.xfail()
9897
def test_invalid_mesh_beam_directions():
99-
_ = get_beam_directions_grid("cubic", 10, mesh="invalid")
98+
with pytest.raises(NotImplementedError):
99+
_ = get_beam_directions_grid("cubic", 10, mesh="invalid")

diffsims/tests/generators/test_simulation_generator.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ def test_init(self, diffraction_calculator: SimulationGenerator):
126126
assert diffraction_calculator.approximate_precession == True
127127
assert diffraction_calculator.minimum_intensity == 1e-20
128128

129+
def test_repr(self, diffraction_calculator):
130+
assert repr(diffraction_calculator) == (
131+
"SimulationGenerator(accelerating_voltage=300, "
132+
"scattering_params=lobato, "
133+
"approximate_precession=True)"
134+
)
135+
129136
def test_matching_results(
130137
self, diffraction_calculator: SimulationGenerator, local_structure
131138
):
@@ -245,35 +252,32 @@ def test_multiphase_multirotation_simulation():
245252
big_silicon = make_phase(10)
246253
rot = Rotation.from_euler([[0, 0, 0], [0.1, 0.1, 0.1]])
247254
rot2 = Rotation.from_euler([[0, 0, 0], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]])
248-
sim = generator.calculate_diffraction2d(
249-
[silicon, big_silicon], rotation=[rot, rot2]
250-
)
255+
_ = generator.calculate_diffraction2d([silicon, big_silicon], rotation=[rot, rot2])
251256

252257

253258
def test_multiphase_multirotation_simulation_error():
254259
generator = SimulationGenerator(300)
255260
silicon = make_phase(5)
256261
big_silicon = make_phase(10)
257262
rot = Rotation.from_euler([[0, 0, 0], [0.1, 0.1, 0.1]])
258-
rot2 = Rotation.from_euler([[0, 0, 0], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]])
263+
_ = Rotation.from_euler([[0, 0, 0], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]])
259264
with pytest.raises(ValueError):
260-
sim = generator.calculate_diffraction2d([silicon, big_silicon], rotation=[rot])
265+
_ = generator.calculate_diffraction2d([silicon, big_silicon], rotation=[rot])
261266

262267

263268
@pytest.mark.parametrize("scattering_param", ["lobato", "xtables"])
264269
def test_param_check(scattering_param):
265-
generator = SimulationGenerator(300, scattering_params=scattering_param)
270+
_ = SimulationGenerator(300, scattering_params=scattering_param)
266271

267272

268-
@pytest.mark.xfail(raises=NotImplementedError)
269273
def test_invalid_scattering_params():
270-
scattering_param = "_empty"
271-
generator = SimulationGenerator(300, scattering_params=scattering_param)
274+
with pytest.raises(NotImplementedError):
275+
_ = SimulationGenerator(300, scattering_params="_empty")
272276

273277

274-
@pytest.mark.xfail(faises=NotImplementedError)
275278
def test_invalid_shape_model():
276-
generator = SimulationGenerator(300, shape_factor_model="dracula")
279+
with pytest.raises(NotImplementedError):
280+
_ = SimulationGenerator(300, shape_factor_model="dracula")
277281

278282

279283
def test_same_simulation_results():

diffsims/tests/library/test_structure_library.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,13 @@ def test_from_orientations_method():
4444
np.testing.assert_equal(library.struct_lib["b"], (2, 4))
4545

4646

47-
@pytest.mark.xfail(reason="Functionality removed")
48-
def test_from_systems_methods():
49-
identifiers = ["a", "b"]
50-
structures = [1, 2]
51-
systems = ["cubic", "hexagonal"]
52-
library = StructureLibrary.from_crystal_systems(
53-
identifiers, structures, systems, resolution=2
54-
)
55-
56-
5747
@pytest.mark.parametrize(
5848
"identifiers, structures, orientations",
5949
[
6050
(["a"], [1, 2], [3, 4]),
6151
(["a"], [1], [3, 4]),
6252
],
6353
)
64-
@pytest.mark.xfail(raises=ValueError)
6554
def test_constructor_parameter_validation_errors(identifiers, structures, orientations):
66-
StructureLibrary(identifiers, structures, orientations)
55+
with pytest.raises(ValueError):
56+
_ = StructureLibrary(identifiers, structures, orientations)

diffsims/tests/patterns/test_detector_functions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ def test_frac_kwarg(self, pattern):
120120
z1 = add_dead_pixels(pattern, fraction=fraction)
121121
assert np.sum(z1 == 0) == 6 # we should have 6 dead pixels!
122122

123-
@pytest.mark.xfail(strict=True)
124123
def test_bad_kwarg_choices_a(self, pattern):
125-
_ = add_dead_pixels(pattern, n=None, fraction=None)
124+
with pytest.raises(ValueError):
125+
_ = add_dead_pixels(pattern, n=None, fraction=None)
126126

127-
@pytest.mark.xfail(strict=True)
128127
def test_bad_kwarg_choices_b(self, pattern):
129-
_ = add_dead_pixels(pattern, n=6, fraction=0.2)
128+
with pytest.raises(ValueError):
129+
_ = add_dead_pixels(pattern, n=6, fraction=0.2)
130130

131131

132132
class TestDetectorGainOffset:

diffsims/tests/sims/test_diffraction_simulation.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import matplotlib.pyplot as plt
1920
import numpy as np
2021
import pytest
2122

@@ -86,6 +87,7 @@ def profile_simulation():
8687

8788
def test_plot_profile_simulation(profile_simulation):
8889
profile_simulation.get_plot()
90+
plt.close()
8991

9092

9193
class TestDiffractionSimulation:
@@ -320,4 +322,6 @@ def test_plot_method(self, units_in):
320322
intensities=np.array([3.0, 5.0, 2.0]),
321323
calibration=[1, 2],
322324
)
323-
ax, sp = short_sim.plot(units=units_in, show_labels=True)
325+
_ = short_sim.plot(units=units_in, show_labels=True)
326+
327+
plt.close()

diffsims/tests/simulations/test_simulations1d.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.
18-
import matplotlib.pyplot as plt
19-
import pytest
2018

21-
from orix.crystal_map import Phase
19+
import matplotlib.pyplot as plt
2220
import numpy as np
21+
from orix.crystal_map import Phase
22+
import pytest
2323

2424
from diffsims.tests.generators.test_simulation_generator import make_phase
2525
from diffsims.simulations import Simulation1D
@@ -57,8 +57,10 @@ def test_init(self, simulation1d):
5757
@pytest.mark.parametrize("with_labels", [True, False])
5858
def test_plot(self, simulation1d, annotate, ax, with_labels):
5959
if ax == "new":
60-
fig, ax = plt.subplots()
61-
fig = simulation1d.plot(annotate_peaks=annotate, ax=ax, with_labels=with_labels)
60+
_, ax = plt.subplots()
61+
_ = simulation1d.plot(annotate_peaks=annotate, ax=ax, with_labels=with_labels)
62+
63+
plt.close()
6264

6365
def test_repr(self, simulation1d):
6466
assert simulation1d.__repr__() == "Simulation1D(name: Al, wavelength: 0.025)"

diffsims/tests/simulations/test_simulations2d.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with diffsims. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import numpy as np
20-
import pytest
21-
2219
from diffpy.structure import Structure, Atom, Lattice
20+
import matplotlib.pyplot as plt
21+
import numpy as np
2322
from orix.crystal_map import Phase
2423
from orix.quaternion import Rotation
24+
import pytest
2525

2626
from diffsims.simulations import Simulation2D
2727
from diffsims.generators.simulation_generator import SimulationGenerator
@@ -80,6 +80,7 @@ def test_iter(self, single_simulation):
8080

8181
def test_plot(self, single_simulation):
8282
single_simulation.plot()
83+
plt.close()
8384

8485
def test_num_rotations(self, single_simulation):
8586
assert single_simulation._num_rotations() == 1
@@ -245,9 +246,11 @@ def test_irot_slice(self, multi_simulation):
245246

246247
def test_plot(self, multi_simulation):
247248
multi_simulation.plot()
249+
plt.close()
248250

249251
def test_plot_rotation(self, multi_simulation):
250252
multi_simulation.plot_rotations()
253+
plt.close()
251254

252255
def test_iter(self, multi_simulation):
253256
multi_simulation.phase_index = 0
@@ -370,8 +373,11 @@ def test_plot(self, multi_simulation, show_labels, units, include_zero_beam):
370373
calibration=0.1,
371374
)
372375

376+
plt.close()
377+
373378
def test_plot_rotation(self, multi_simulation):
374379
multi_simulation.plot_rotations()
380+
plt.close()
375381

376382
def test_iter(self, multi_simulation):
377383
multi_simulation.phase_index = 0

diffsims/tests/utils/test_discretise_utils.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,10 @@ def test_getA(Z, returnFunc):
6262
assert len(a) == len(b)
6363

6464

65-
@pytest.mark.xfail(raises=ValueError)
66-
def test_2_atoms_getA():
67-
get_atoms(np.array([0, 1]))
68-
69-
70-
@pytest.mark.xfail(raises=ValueError)
71-
def test_max_atom_getA():
72-
get_atoms("Es")
65+
@pytest.mark.parametrize("atoms", [np.array([0, 1]), "Es"])
66+
def tests_get_atoms_raises(atoms):
67+
with pytest.raises(ValueError):
68+
get_atoms(atoms)
7369

7470

7571
@pytest.mark.parametrize(
@@ -82,13 +78,7 @@ def test_max_atom_getA():
8278
)
8379
def test_rebin(r):
8480
x = [np.linspace(0, 1, 10), np.linspace(0, 1, 21), np.linspace(0, 1, 32)]
85-
loc = np.concatenate(
86-
[
87-
0 * np.linspace(0, 1, 500)[:, None],
88-
]
89-
* 3,
90-
axis=1,
91-
)
81+
loc = np.concatenate([0 * np.linspace(0, 1, 500)[:, None]] * 3, axis=1)
9282
k = 1
9383
mem = 1e5
9484
rebin(x, loc, r, k, mem)

diffsims/tests/utils/test_fourier_transform.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ def test_DFT(rX, rY):
206206
np.testing.assert_allclose(f, IFT(g, axes=axes), 1e-5, 1e-5)
207207

208208

209-
@pytest.mark.xfail(raises=ValueError)
210209
def test_fail_DFT():
211-
get_DFT()
210+
with pytest.raises(ValueError):
211+
get_DFT()
212212

213213

214214
@pytest.mark.parametrize(

diffsims/tests/utils/test_probe_utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def _random_array(shape, n=0):
8585
return x.reshape(shape)
8686

8787

88-
@pytest.mark.xfail(raises=NotImplementedError)
8988
def test_null_probe():
9089
p = ProbeFunction()
91-
p(1)
90+
with pytest.raises(NotImplementedError):
91+
p(1)
9292

9393

9494
@pytest.fixture(params=[(10, 11, 12)])

diffsims/tests/utils/test_sim_utils.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,25 @@ def test_kinematic_simulator_xtables_scattering_params():
114114
)
115115

116116

117-
@pytest.mark.xfail(raises=NotImplementedError)
118117
def test_kinematic_simulator_invalid_scattering_params():
119118
atomic_coordinates = np.asarray([[0, 0, 0]]) # structure.cart_coords
120-
sim = simulate_kinematic_scattering(
121-
atomic_coordinates,
122-
"Si",
123-
300.0,
124-
simulation_size=32,
125-
illumination="gaussian_probe",
126-
scattering_params="_empty",
127-
)
119+
with pytest.raises(NotImplementedError):
120+
simulate_kinematic_scattering(
121+
atomic_coordinates,
122+
"Si",
123+
300.0,
124+
simulation_size=32,
125+
illumination="gaussian_probe",
126+
scattering_params="_empty",
127+
)
128128

129129

130-
@pytest.mark.xfail(raises=ValueError)
131130
def test_kinematic_simulator_invalid_illumination():
132131
atomic_coordinates = np.asarray([[0, 0, 0]]) # structure.cart_coords
133-
sim = simulate_kinematic_scattering(
134-
atomic_coordinates, "Si", 300.0, simulation_size=32, illumination="gaussian"
135-
)
132+
with pytest.raises(ValueError):
133+
simulate_kinematic_scattering(
134+
atomic_coordinates, "Si", 300.0, simulation_size=32, illumination="gaussian"
135+
)
136136

137137

138138
@pytest.mark.parametrize(

diffsims/tests/utils/test_vector_utils.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import numpy as np
2020
import pytest
2121

22-
from diffsims.utils.vector_utils import get_angle_cartesian
23-
from diffsims.utils.vector_utils import get_angle_cartesian_vec
22+
from diffsims.utils.vector_utils import get_angle_cartesian, get_angle_cartesian_vec
2423

2524

2625
@pytest.mark.parametrize(
@@ -47,6 +46,6 @@ def test_get_angle_cartesian_vec(a, b, expected_angles):
4746
np.testing.assert_allclose(angles, expected_angles)
4847

4948

50-
@pytest.mark.xfail(raises=ValueError)
5149
def test_get_angle_cartesian_vec_input_validation():
52-
get_angle_cartesian_vec(np.empty((2, 3)), np.empty((5, 3)))
50+
with pytest.raises(ValueError):
51+
get_angle_cartesian_vec(np.empty((2, 3)), np.empty((5, 3)))

0 commit comments

Comments
 (0)