Skip to content

Commit 56b4936

Browse files
committed
test error messages
1 parent e766122 commit 56b4936

File tree

20 files changed

+72
-71
lines changed

20 files changed

+72
-71
lines changed

pymatgen/analysis/tests/test_structure_matcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def test_get_mapping(self):
580580
# s2 is smaller than s1
581581
del s2[0]
582582
del s2[1]
583-
with pytest.raises(ValueError):
583+
with pytest.raises(ValueError, match="subset is larger than superset"):
584584
sm.get_mapping(s2, s1)
585585

586586
def test_get_supercell_matrix(self):

pymatgen/analysis/xas/tests/test_spectrum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_validate(self):
7878
)
7979

8080
def test_stitch_xafs(self):
81-
with pytest.raises(ValueError):
81+
with pytest.raises(ValueError, match="Invalid mode. Only XAFS and L23 are supported"):
8282
XAS.stitch(self.k_xanes, self.k_exafs, mode="invalid")
8383
xafs = XAS.stitch(self.k_xanes, self.k_exafs, mode="XAFS")
8484
assert isinstance(xafs, XAS)
@@ -110,7 +110,7 @@ def test_stitch_l23(self):
110110
assert np.greater_equal(l23.y, self.l2_xanes.y).all()
111111
assert len(l23.x) == 100
112112
self.l2_xanes.spectrum_type = "EXAFS"
113-
with pytest.raises(ValueError):
113+
with pytest.raises(ValueError, match="Only XANES spectrum can be stitched in L23 mode"):
114114
XAS.stitch(self.l2_xanes, self.l3_xanes, mode="L23")
115115
self.l2_xanes.absorbing_element = Element("Pt")
116116
with pytest.raises(ValueError, match="The absorbing elements for spectra are different"):

pymatgen/apps/battery/tests/test_analyzer.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def load_from_internal(self, name, oxidations, working_ion="Li"):
2222
return BatteryAnalyzer(struct, working_ion)
2323

2424
def setUp(self):
25-
self.lifepo4 = self.load_from_internal("LiFePO4", {"Li": 1, "Fe": 2, "P": 5, "O": -2})
26-
self.nafepo4 = self.load_from_internal("NaFePO4", {"Na": 1, "Fe": 2, "P": 5, "O": -2}, working_ion="Na")
25+
self.li_fe_p_o4 = self.load_from_internal("LiFePO4", {"Li": 1, "Fe": 2, "P": 5, "O": -2})
26+
self.na_fe_p_o4 = self.load_from_internal("NaFePO4", {"Na": 1, "Fe": 2, "P": 5, "O": -2}, working_ion="Na")
2727
self.la2coo4f = self.load_from_internal("La2CoO4F", {"La": 3, "Co": 3, "O": -2, "F": -1}, working_ion="F")
28-
self.fepo4 = self.load_from_cif("FePO4a.cif", {"Fe": 3, "P": 5, "O": -2})
28+
self.fe_p_o4 = self.load_from_cif("FePO4a.cif", {"Fe": 3, "P": 5, "O": -2})
2929
self.la2coo4 = self.load_from_cif("La2CoO4.cif", {"La": 3, "Co": 2, "O": -2}, working_ion="F")
3030
self.lifemnpo4 = self.load_from_cif("Li4Fe3Mn1(PO4)4.cif", {"Li": 1, "Fe": 2, "Mn": 2, "P": 5, "O": -2})
3131
self.li8nicofe208 = self.load_from_cif(
@@ -34,55 +34,55 @@ def setUp(self):
3434
self.li3v2p3o12 = self.load_from_internal("Li3V2(PO4)3", {"Li": 1, "V": 3, "O": -2, "P": 5})
3535
self.mgnif6 = self.load_from_cif("MgNiF6.cif", {"Mg": 2, "Ni": 4, "F": -1}, working_ion="F")
3636

37-
def test_oxid_check(self):
37+
def test_oxide_check(self):
3838
struct = self.get_structure("LiFePO4")
39-
with pytest.raises(ValueError):
39+
with pytest.raises(ValueError, match="BatteryAnalyzer requires oxidation states assigned to structure"):
4040
BatteryAnalyzer(struct, "Li")
4141

4242
def test_capacitygrav_calculations(self):
43-
lifepo4_cap = 169.89053 # same as fepo4 cap
44-
nafepo4_cap = 154.20331
45-
la2coo4f_cap = 175.6564
46-
li3v2p3o12_cap_remove = 197.25339
47-
li3v2p3o12_cap_insert = 127.17129
43+
li_fe_p_o4_cap = 169.89053 # same as fe_po4 cap
44+
na_fe_p_o4_cap = 154.20331
45+
la2_co_o4_f_cap = 175.6564
46+
li3_v2_p3_o12_cap_remove = 197.25339
47+
li3_v2_p3_o12_cap_insert = 127.17129
4848

49-
assert self.lifepo4.get_max_capgrav() == approx(lifepo4_cap, abs=1e-3)
50-
assert self.lifepo4.get_max_capgrav(remove=False) == 0
51-
assert self.lifepo4.get_max_capgrav(insert=False) == approx(lifepo4_cap, abs=1e-3)
49+
assert self.li_fe_p_o4.get_max_capgrav() == approx(li_fe_p_o4_cap, abs=1e-3)
50+
assert self.li_fe_p_o4.get_max_capgrav(remove=False) == 0
51+
assert self.li_fe_p_o4.get_max_capgrav(insert=False) == approx(li_fe_p_o4_cap, abs=1e-3)
5252

53-
assert self.nafepo4.get_max_capgrav() == approx(nafepo4_cap, abs=1e-3)
54-
assert self.nafepo4.get_max_capgrav(remove=False) == 0
53+
assert self.na_fe_p_o4.get_max_capgrav() == approx(na_fe_p_o4_cap, abs=1e-3)
54+
assert self.na_fe_p_o4.get_max_capgrav(remove=False) == 0
5555

56-
assert self.fepo4.get_max_capgrav() == approx(lifepo4_cap, abs=1e-3)
57-
assert self.fepo4.get_max_capgrav(insert=False) == 0
56+
assert self.fe_p_o4.get_max_capgrav() == approx(li_fe_p_o4_cap, abs=1e-3)
57+
assert self.fe_p_o4.get_max_capgrav(insert=False) == 0
5858

59-
assert self.la2coo4f.get_max_capgrav() == approx(la2coo4f_cap, abs=1e-3)
60-
assert self.la2coo4.get_max_capgrav() == approx(la2coo4f_cap, abs=1e-3)
59+
assert self.la2coo4f.get_max_capgrav() == approx(la2_co_o4_f_cap, abs=1e-3)
60+
assert self.la2coo4.get_max_capgrav() == approx(la2_co_o4_f_cap, abs=1e-3)
6161
assert self.la2coo4.get_max_capgrav(insert=False) == 0
6262

63-
assert self.li3v2p3o12.get_max_capgrav(insert=False) == approx(li3v2p3o12_cap_remove, abs=1e-3)
64-
assert self.li3v2p3o12.get_max_capgrav(remove=False) == approx(li3v2p3o12_cap_insert, abs=1e-3)
63+
assert self.li3v2p3o12.get_max_capgrav(insert=False) == approx(li3_v2_p3_o12_cap_remove, abs=1e-3)
64+
assert self.li3v2p3o12.get_max_capgrav(remove=False) == approx(li3_v2_p3_o12_cap_insert, abs=1e-3)
6565

6666
def test_capacityvol_calculations(self):
67-
lifepo4_cap = 594.17518
68-
nafepo4_cap = 542.86104
67+
li_fe_p_o4_cap = 594.17518
68+
na_fe_p_o4_cap = 542.86104
6969

70-
fepo4_cap = 624.82289 # this is different than lifepo4 cap if lifepo4 volume not known
70+
fe_p_o4_cap = 624.82289 # this is different than li_fe_p_o4_cap cap if li_fe_p_o4 volume not known
7171

72-
assert self.lifepo4.get_max_capvol() == approx(lifepo4_cap, abs=1e-3)
73-
assert self.lifepo4.get_max_capvol(remove=False) == 0
74-
assert self.lifepo4.get_max_capvol(insert=False) == approx(lifepo4_cap, abs=1e-3)
72+
assert self.li_fe_p_o4.get_max_capvol() == approx(li_fe_p_o4_cap, abs=1e-3)
73+
assert self.li_fe_p_o4.get_max_capvol(remove=False) == 0
74+
assert self.li_fe_p_o4.get_max_capvol(insert=False) == approx(li_fe_p_o4_cap, abs=1e-3)
7575

76-
assert self.nafepo4.get_max_capvol() == approx(nafepo4_cap, abs=1e-3)
77-
assert self.nafepo4.get_max_capvol(remove=False) == 0
78-
assert self.nafepo4.get_max_capvol(insert=False) == approx(nafepo4_cap, abs=1e-3)
76+
assert self.na_fe_p_o4.get_max_capvol() == approx(na_fe_p_o4_cap, abs=1e-3)
77+
assert self.na_fe_p_o4.get_max_capvol(remove=False) == 0
78+
assert self.na_fe_p_o4.get_max_capvol(insert=False) == approx(na_fe_p_o4_cap, abs=1e-3)
7979

80-
assert self.fepo4.get_max_capvol() == approx(fepo4_cap, abs=1e-3)
81-
assert self.fepo4.get_max_capvol(remove=False) == approx(fepo4_cap, abs=1e-3)
82-
assert self.fepo4.get_max_capvol(insert=False) == 0
80+
assert self.fe_p_o4.get_max_capvol() == approx(fe_p_o4_cap, abs=1e-3)
81+
assert self.fe_p_o4.get_max_capvol(remove=False) == approx(fe_p_o4_cap, abs=1e-3)
82+
assert self.fe_p_o4.get_max_capvol(insert=False) == 0
8383

8484
# give the lifepo4 volume, should get lifepo4 capacity
85-
assert self.fepo4.get_max_capvol(volume=self.lifepo4.struc_oxid.volume) == approx(lifepo4_cap, abs=1e-3)
85+
assert self.fe_p_o4.get_max_capvol(volume=self.li_fe_p_o4.struc_oxid.volume) == approx(li_fe_p_o4_cap, abs=1e-3)
8686

8787
def test_ion_removal(self):
8888
assert self.lifemnpo4.get_removals_int_oxid() == {1, 2, 3, 4}

pymatgen/command_line/gulp_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def readable(f):
373373
gin = "library " + file_name
374374
if gin:
375375
return gin + "\n"
376-
raise GulpError("GULP Library not found")
376+
raise GulpError("GULP library not found")
377377

378378
def buckingham_input(self, structure: Structure, keywords, library=None, uc=True, valence_dict=None):
379379
"""

pymatgen/command_line/tests/test_enumlib_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_rounding_errors(self):
8686
# This has been fixed by multiplying the base by 100.
8787
struct = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu7Te5.cif"))
8888
adaptor = EnumlibAdaptor(struct, 1, 2)
89-
with pytest.raises(EnumError):
89+
with pytest.raises(EnumError, match="Unable to enumerate structure"):
9090
adaptor.run()
9191
adaptor = EnumlibAdaptor(struct, 1, 5)
9292
adaptor.run()

pymatgen/command_line/tests/test_gulp_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_library_line_explicit_path(self):
137137
assert "lib" in gin
138138

139139
def test_library_line_wrong_file(self):
140-
with pytest.raises(GulpError):
140+
with pytest.raises(GulpError, match="GULP library not found"):
141141
self.gio.library_line("temp_to_fail.lib")
142142

143143
def test_buckingham_potential(self):

pymatgen/core/tests/test_periodic_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def test_sort(self):
545545

546546
def test_immutable(self):
547547
sp = Species("Fe", 2, spin=5)
548-
with pytest.raises(AttributeError):
548+
with pytest.raises(AttributeError, match="property 'spin' of 'Species' object has no setter"):
549549
sp.spin = 6
550550
sp.properties["spin"] = 7
551551
assert sp.spin == 5

pymatgen/electronic_structure/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Spin(Enum):
1818
Usage: Spin.up, Spin.down.
1919
"""
2020

21-
up, down = (1, -1)
21+
up, down = 1, -1
2222

2323
def __int__(self):
2424
return self.value

pymatgen/electronic_structure/tests/test_cohp.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_get_interpolated_value(self):
6363
icoop_ef = self.coop.get_interpolated_value(self.coop.efermi, integrated=True)
6464
assert icohp_ef_dict == approx(icohp_ef)
6565
assert icoop_ef_dict == approx(icoop_ef)
66-
with pytest.raises(ValueError):
66+
with pytest.raises(ValueError, match="ICOHP is empty"):
6767
self.cohp_only.get_interpolated_value(5.0, integrated=True)
6868

6969
def test_str(self):
@@ -1057,9 +1057,10 @@ def test_get_summed_cohp_by_label_and_orbital_list(self):
10571057
assert_array_equal(cohp_label2.icohp[Spin.up], ref["ICOHP"][Spin.up] * 2.0)
10581058
assert_array_equal(cohp_label2x.icohp[Spin.up], ref["ICOHP"][Spin.up])
10591059
assert_array_equal(cohp_label3.icohp[Spin.up], ref["ICOHP"][Spin.up] + ref2["ICOHP"][Spin.up])
1060-
with pytest.raises(ValueError):
1060+
expected_msg = "label_list and orbital_list don't have the same length"
1061+
with pytest.raises(ValueError, match=expected_msg):
10611062
self.cohp_orb.get_summed_cohp_by_label_and_orbital_list(["1"], ["4px-4pz", "4s-4px"])
1062-
with pytest.raises(ValueError):
1063+
with pytest.raises(ValueError, match=expected_msg):
10631064
self.cohp_orb.get_summed_cohp_by_label_and_orbital_list(["1", "2"], ["4s-4px"])
10641065

10651066
def test_get_summed_cohp_by_label_and_orbital_list_summed_spin_channels(self):
@@ -1085,11 +1086,12 @@ def test_get_summed_cohp_by_label_and_orbital_list_summed_spin_channels(self):
10851086
assert_array_equal(cohp_label2.icohp[Spin.up], ref["ICOHP"][Spin.up] * 2.0)
10861087
assert_array_equal(cohp_label2x.icohp[Spin.up], ref["ICOHP"][Spin.up])
10871088
assert_array_equal(cohp_label3.icohp[Spin.up], ref["ICOHP"][Spin.up] + ref2["ICOHP"][Spin.up])
1088-
with pytest.raises(ValueError):
1089+
expected_msg = "label_list and orbital_list don't have the same length"
1090+
with pytest.raises(ValueError, match=expected_msg):
10891091
self.cohp_orb.get_summed_cohp_by_label_and_orbital_list(
10901092
["1"], ["4px-4pz", "4s-4px"], summed_spin_channels=True
10911093
)
1092-
with pytest.raises(ValueError):
1094+
with pytest.raises(ValueError, match=expected_msg):
10931095
self.cohp_orb.get_summed_cohp_by_label_and_orbital_list(["1", "2"], ["4s-4px"], summed_spin_channels=True)
10941096

10951097
# files with spin polarization

pymatgen/electronic_structure/tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_init(self):
1717
def test_from_int(self):
1818
assert Spin(1) == Spin.up
1919
assert Spin(-1) == Spin.down
20-
with pytest.raises(ValueError):
20+
with pytest.raises(ValueError, match="0 is not a valid Spin"):
2121
Spin(0)
2222

2323
def test_cached(self):

0 commit comments

Comments
 (0)