问题描述
我正在使用 pytest 编写测试。我有一个案例,如果输入错误,某些函数会在终端上抛出 SystemExit
并带有一些错误消息。
我想为抛出 SystemExit
的情况编写测试并验证输出错误消息中是否存在特定字符串。
代码如下:
def test_validate_input():
...
with pytest.raises(SystemExit) as error:
_validate_function(test_wrong_input)
assert error.value.code == 1
如果我运行验证某些输入的实际函数,我将无法在命令行上获得 error
中的输出错误消息。请让我知道我在这里缺少什么。
编辑:
我正在调用 subprocess.call_output
来运行抛出错误的命令。我必须在 stderr=subprocess.STDOUT
调用中添加 call_output
作为获取错误消息的参数。然后我在测试中使用了@p3j4p5 的答案。
解决方法
Pytest 的 raises()
接受一个 match
参数。文档告诉我们:
如果指定,则包含正则表达式的字符串或正则表达式对象,针对异常的字符串表示进行测试
和
这仅在 pytest.raises
用作上下文管理器时使用
所以它应该适合你的情况:
def test_validate_input():
...
with pytest.raises(SystemExit,match='exception message content'):
_validate_function(test_wrong_input)
如果引发的 SystemExit
异常已通过与提供的正则表达式匹配的消息引发,则此测试将通过,否则将失败。
或者,如果您想手动检查消息:
上下文管理器生成一个 ExceptionInfo
对象,可用于检查捕获的异常的详细信息
在您的情况下,假设 SystemExit
使用整数(代码)和字符串(消息)调用:
def test_validate_input():
...
with pytest.raises(SystemExit) as exc_info:
_validate_function(test_wrong_input)
assert exc_info.value.args[0] == 1
assert exc_info.value.args[1] == 'exception message content'
,
如果我理解正确,您的消息会在 之前打印在 stderr 上,SystemExit
在这种情况下,您需要 capsys
def test_validate_input(capsys):
...
with pytest.raises(SystemExit) as error:
_validate_function(test_wrong_input)
assert error.value.code == 1
captured = capsys.readouterr()
assert captured.err == "Expected error message\n"