官术网_书友最值得收藏!

Checking exception messages

As stated at the start of this section, APIs should provide clear messages in the exceptions they raise. In the previous examples, we only verified that the code was raising the appropriate exception type, but not the actual message.

pytest.raises can receive an optional match argument, which is a regular expression string that will be matched against the exception message, as well as checking the exception type. For more details, go to: https://docs.python.org/3/howto/regex.html. We can use that to improve our tests even further:

def test_empty_name():
with pytest.raises(InvalidCharacterNameError,
match='character name empty'):
create_character(name='', class_name='warrior')


def test_invalid_class_name():
with pytest.raises(InvalidClassNameError,
match='invalid class name: "mage"'):
create_character(name='Solaire', class_name='mage')

Simple!

主站蜘蛛池模板: 肇庆市| 双牌县| 泽普县| 留坝县| 林芝县| 礼泉县| 拉萨市| 罗甸县| 于都县| 呼和浩特市| 扶余县| 报价| 景泰县| 德州市| 吉林省| 武威市| 淳安县| 铜梁县| 沈阳市| 普定县| 浮梁县| 九龙城区| 昌邑市| 高雄县| 通化县| 海宁市| 龙陵县| 通榆县| 宝丰县| 松桃| 南昌市| 惠东县| 宁乡县| 黔江区| 丁青县| 武定县| 石柱| 黑山县| 花垣县| 辰溪县| 抚远县|