Python:断言用户警告
Python: Assert a UserWarning
我创建了自己的 python 脚本 script.py
,我在其中定义了自己的 class(例如 myclass
)。 class 将 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.")
我正在尝试 verify/assert 通过 运行 以下单元测试发出警告消息:
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'>
MrBean Bremen 回答了这个问题。
解决办法是把pytest.raises
改成pytest.warns
。
我创建了自己的 python 脚本 script.py
,我在其中定义了自己的 class(例如 myclass
)。 class 将 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.")
我正在尝试 verify/assert 通过 运行 以下单元测试发出警告消息:
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'>
MrBean Bremen 回答了这个问题。
解决办法是把pytest.raises
改成pytest.warns
。