简体   繁体   中英

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

I cannot seem to get pytest to work on my custom Exception.

Here's the exception code:

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."

Following the doc , here's (simplified) test function:

from pyupurs.exceptions import FileIsEmptyError

...

with pytest.raises(FileIsEmptyError):
    raise FileIsEmptyError

Here's the unit test's return:

with pytest.raises(FileIsEmptyError): E TypeError: exceptions must be derived from BaseException, not class 'module'

Here's the project structure:

pyupurs
|---pyupurs
   |---exceptions
      |---__init__.py
      |---FileIsEmptyError.py
   |--- stateless_file_ops
|--- tests
   |--- pyupurs
      |--- stateless_file_ops
         |--- __init__.py
         |--- test_auditing_ops.py
      |--- __init__.py
   |--- samples
      |--- ... 
|--- ...

Change this

from pyupurs.exceptions import FileIsEmptyError

to this

from pyupurs.exceptions.FileIsEmptyError import FileIsEmptyError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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