Mock object but also assert method calls?
thomas.lehmann.private at googlemail.com
Sat Aug 15 13:07:21 CEST 2015
Am Freitag, 14. August 2015 04:53:56 UTC+2 schrieb Steven D'Aprano:
> On Fri, 14 Aug 2015 07:21 am, Ben Finney wrote:
> >> If find following totally different to the normal API which
> >> is provided by the mock library:
> >> assert call().test2("hello") in mocked_objects.mock_calls
> > The 'assert' statement is a crude tool, which knows little about the
> > intent of your assertion.
> I agree with Ben here. Despite the popularity of "nose" (I think it is
> nose?) which uses `assert` for testing, I think that is a gross misuse of
> the statement. It is okay to use assertions this way for quick and dirty ad
> hoc testing, say at the command line, but IMO totally inappropriate for
> anything more formal, like unit testing.
> If for no other reason than the use of `assert` for testing makes it
> impossible to test your code when running with the Python -O (optimize)
> For more detail on the uses, and abuses, of `assert` see this:
Of course you do NOT use assert in unit tests but I provided
just test code to show my problem. Of course I take nose and
hamcrest and code coverage ...
Here a complete example of my problem (with comments):
from mock import MagicMock, call, patch
def test1(self, value):
mocked_object = MagicMock(Bla)
bla = Bla()
# empty, why?
# does not work: mocked_object.test1.assert_called_with("hello")
with patch("__main__.Bla") as mocked_object:
bla = Bla()
# not empty!
# does also not work: mocked_object.test1.assert_called_with("hello")
# but this does work:
assert call().test1("hello") in mocked_object.mock_calls
I don't wanna patch each individual method. Is there no other way?
More information about the Python-list