Imad
Imad

Reputation: 2741

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

Upvotes: 1

Views: 1329

Answers (1)

Akın Tekeoğlu
Akın Tekeoğlu

Reputation: 161

Change this

from pyupurs.exceptions import FileIsEmptyError

to this

from pyupurs.exceptions.FileIsEmptyError import FileIsEmptyError

Upvotes: 1

Related Questions