This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mocking an exception, arguments do not seem to be passed to the mock
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, lisroach, mariocj89, michael.foord, piscvau, xtreak
Priority: normal Keywords:

Created on 2019-11-09 20:19 by piscvau, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
essai mock_exception.py piscvau, 2019-11-09 20:19 source file
Messages (5)
msg356305 - (view) Author: Troulet-lambert Odile (piscvau) Date: 2019-11-09 20:19
When patching an Exception with a side_effect to another exception, it seems like the exceiption arguments are not passed to the mock.
msg356314 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-11-10 04:49
Currently, the exception is not instantiated. Maybe we can check if it's callable and pass args, kwargs to the exception constructor to be raised.

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index a48132c5b1..f5bcb911f5 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1145,7 +1145,10 @@ class CallableMixin(Base):
         effect = self.side_effect
         if effect is not None:
             if _is_exception(effect):
-                raise effect
+                if _callable(effect):
+                    raise effect(*args, **kwargs)
+                else:
+                    raise effect
             elif not _callable(effect):
                 result = next(effect)
                 if _is_exception(result):
msg356320 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-11-10 07:00
Not sure this is correct, if an effect is an exception and requires args, then it should be passed as an instance, not a class:

Mock(side_effect=MyException(‘foo’))

> On 10 Nov 2019, at 04:49, Karthikeyan Singaravelan <report@bugs.python.org> wrote:
> 
> 
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
> 
> Currently, the exception is not instantiated. Maybe we can check if it's callable and pass args, kwargs to the exception constructor to be raised.
> 
> diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
> index a48132c5b1..f5bcb911f5 100644
> --- a/Lib/unittest/mock.py
> +++ b/Lib/unittest/mock.py
> @@ -1145,7 +1145,10 @@ class CallableMixin(Base):
>         effect = self.side_effect
>         if effect is not None:
>             if _is_exception(effect):
> -                raise effect
> +                if _callable(effect):
> +                    raise effect(*args, **kwargs)
> +                else:
> +                    raise effect
>             elif not _callable(effect):
>                 result = next(effect)
>                 if _is_exception(result):
> 
> ----------
> nosy: +cjw296, lisroach, mariocj89, michael.foord
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38757>
> _______________________________________
msg356321 - (view) Author: Mario Corchero (mariocj89) * (Python triager) Date: 2019-11-10 09:39
The reason why it seems that "no arguments are beeing passed" is because the exception is not being raised by you, but by mock itself when the exception is trying to be created. When an exception type is passed as side_effect, the mock modules raises such exception on the callable (the creation of the initial exception) To confirm this, just try removing the "raise" and leave the creation of the exception only.

I'd suggest that you use `wraps` rather than `side_effect`. That will make your example work.

Alternatively, if you need to use `side_effect`, you can use a wrapper so mock won't raise an exception when it sees that one:

```

def wrapper(*args, **kwargs):
    return MockError(*args, **kwargs)

patcher = patch('__main__.ProductionError', side_effect=wrapper)

```


I think this can be closed as a non-issue.
msg356360 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-11-11 12:49
Agreed.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82938
2019-11-11 12:49:34cjw296setstatus: open -> closed
resolution: not a bug
messages: + msg356360

stage: resolved
2019-11-10 09:39:21mariocj89setmessages: + msg356321
2019-11-10 07:00:43cjw296setmessages: + msg356320
2019-11-10 04:49:26xtreaksetnosy: + cjw296, michael.foord, lisroach, mariocj89
messages: + msg356314
2019-11-10 02:44:44xtreaksetnosy: + xtreak
2019-11-09 20:19:55piscvaucreate