我似乎无法让pytest处理我的自定义异常
以下是异常代码:
class FileIsEmptyError(Exception):
"""Raised when the input file is empty"""
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None
def __str__(self):
if self.message:
return f"FileIsEmptyError, {self.message}"
else:
return f"FileIsEmptyError has been raised."
在doc之后,下面是(简化的)测试函数:
from pyupurs.exceptions import FileIsEmptyError
...
with pytest.raises(FileIsEmptyError):
raise FileIsEmptyError
下面是单元测试的回报:
with pytest.raises(FileIsEmptyError): E TypeError: exceptions must be derived from BaseException, not class 'module'
以下是项目结构:
pyupurs
|---pyupurs
|---exceptions
|---__init__.py
|---FileIsEmptyError.py
|--- stateless_file_ops
|--- tests
|--- pyupurs
|--- stateless_file_ops
|--- __init__.py
|--- test_auditing_ops.py
|--- __init__.py
|--- samples
|--- ...
|--- ...
改变这个
from pyupurs.exceptions import FileIsEmptyError
对此
from pyupurs.exceptions.FileIsEmptyError import FileIsEmptyError
相关问题 更多 >
编程相关推荐