Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95391
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-08-15 04:07 -0700 |
| References | <509adc48-996d-44b0-abf1-e15ee387023b@googlegroups.com> <mailman.178.1439500927.3627.python-list@python.org> <55cd5839$0$1636$c3e8da3$5496439d@news.astraweb.com> |
| Message-ID | <cca7ce43-bed9-48a9-959a-341b17e5cebf@googlegroups.com> (permalink) |
| Subject | Re: Mock object but also assert method calls? |
| From | Thomas Lehmann <thomas.lehmann.private@googlemail.com> |
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)
> switch.
>
> 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
class Bla:
def test1(self, value):
pass
mocked_object = MagicMock(Bla)
bla = Bla()
bla.test1("hello")
# empty, why?
print(mocked_object.mock_calls)
# does not work: mocked_object.test1.assert_called_with("hello")
with patch("__main__.Bla") as mocked_object:
bla = Bla()
bla.test1("hello")
# not empty!
print(mocked_object.mock_calls)
# 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?
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Mock object but also assert method calls? Thomas Lehmann <thomas.lehmann.private@googlemail.com> - 2015-08-13 08:58 -0700
Re: Mock object but also assert method calls? Ben Finney <ben+python@benfinney.id.au> - 2015-08-14 07:21 +1000
Re: Mock object but also assert method calls? Steven D'Aprano <steve@pearwood.info> - 2015-08-14 12:53 +1000
Re: Mock object but also assert method calls? Thomas Lehmann <thomas.lehmann.private@googlemail.com> - 2015-08-15 04:07 -0700
csiph-web