4

在 pytest 中断言 UserWarning 和 SystemExit

在我的应用程序中,我有一个函数,当提供错误的参数值时,它将从模块引发一个UserWarnings然后warningsSystemExit模块引发sys

代码类似于:

def compare_tags(.....):

    requested_tags = user_requested_tags # as list
    all_tags = tags_calculated_from_input_file  # as list 

    non_matching_key = [x for x in requested_tags if x not in all_tags]

    # if user requested non existing tag then raise warning and then exit 
    if len(non_matching_key) > 0:

        # generate warning
        warnings.warn("The requested '%s' keys from '%s' is not present in the input file. Please makes sure the input file has the metadata of interest or remove the non matching keys." %(non_matching_key, given_tags))

        # raise system exit
        sys.exit(0)

为上述函数编写 pytest

我想立即在 pytest 中UserWarning进行测试。SystemExit我可以SystemExit在 pytest 中进行检查。

with pytest.raises(SystemExit):
    compare_tags(....)

但这也会显示警告消息(这不是错误)。

如果我想检查警告:

pytest.warns(UserWarning, 
    compare_tags(...)

这会产生一个SystemExit错误,因为这个被调用的函数会触发系统退出。

我怎样才能把warningsSystemExit检查都放在同一个pytest中?

4

2 回答 2

4

pytest.warnspytest.raises是常用的上下文管理器,可以在with用逗号分隔的单个语句中声明(参见复合语句):

with pytest.warns(UserWarning), pytest.raises(SystemExit):
    compare_tags(...)

这实际上与写作相同

with pytest.warns(UserWarning):
    with pytest.raises(SystemExit):
        compare_tags(...)

请注意,顺序很重要- 当您以相反的顺序放置两个上下文管理器时:

with pytest.raises(SystemExit), pytest.warns(UserWarning):
    ...

这和写作一样

with pytest.raises(SystemExit):
    with pytest.warns(UserWarning):
        ...

这里的问题是pytest.raises它将捕获所有引发的错误,然后检查捕获的内容。这包括pytest.warns加注。这意味着

with pytest.raises(SystemExit), pytest.warns(UserWarning):
    sys.exit(0)

将通过,因为引发的错误pytest.warns将被吞噬pytest.raises,而

with pytest.warns(UserWarning), pytest.raises(SystemExit):
    sys.exit(0)

将按预期失败。

于 2019-09-28T09:05:12.260 回答
2

您可以像这样嵌套两个异常:

def test_exit():
    with pytest.raises(SystemExit):
        error_msg = "warning here"
        with pytest.warns(UserWarning, match = error_msg):
            compare_tags(...)
于 2019-09-28T07:38:33.653 回答