繁体   English   中英

Pytest & 自定义异常:TypeError:异常必须从 BaseException 派生,而不是 class 'module'

[英]Pytest & custom exception : TypeError: exceptions must be derived from BaseException, not class 'module'

我似乎无法让 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."

文档之后,这里是(简化的)测试 function:

from pyupurs.exceptions import FileIsEmptyError

...

with pytest.raises(FileIsEmptyError):
    raise FileIsEmptyError

这是单元测试的返回:

使用 pytest.raises(FileIsEmptyError): E TypeError: 异常必须从 BaseException 派生,而不是 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM