Python:断言用户警告

时间:2021-01-04 11:19:34

标签: python python-unittest

我创建了自己的 Python 脚本 script.py,在其中定义了自己的类(例如 myclass)。该类读取一个 csv 文件作为数据帧。我建立的其中一项检查是确保数据集中存在特定列。支票是:

if ("column" not in self.df.columns): warnings.warn("column is not available in the input file.")

这实际上工作正常,当 "column" 未包含在数据集中时,会​​产生警告错误:

UserWarning: column is not available in the input file.
  if ("column" not in self.df.columns): warnings.warn("column is not available in the input file.")

我正在尝试通过运行以下单元测试来验证/断言警告消息的问题:

import script
...
...
...
def test_if_error_is_issued ():
    with pytest.raises(UserWarning) as warn:
        script.myclass(path)

我认为后一个代码块不正确,因为我不断收到以下错误消息(path 实际上导致没有“列”的 csv):

E           Failed: DID NOT RAISE <class 'UserWarning'>

1 个答案:

答案 0 :(得分:0)

MrBean Bremen 回答了这个问题。

解决方案是将 var imagebutton = new ImageButton {Source = "/drawable/delete", HorizontalOptions = LayoutOptions.End, HeightRequest = 60, BackgroundColor = Color.Red, Padding = new Thickness(20,-5), CornerRadius = 45} var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += (s, e) => { // handle the tap }; imagebutton .GestureRecognizers.Add(tapGestureRecognizer); Content = new StackLayout { Children = { imagebutton } } 更改为 pytest.raises

相关问题