Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95358

Re: Mock object but also assert method calls?

Path csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"""': 0.05; 'defaults': 0.05; 'attributes': 0.07; 'tool,': 0.07; 'api': 0.09; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'def': 0.13; '8bit%:26': 0.16; 'class;': 0.16; 'crude': 0.16; 'lehmann': 0.16; 'methods;': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:object': 0.16; 'attribute': 0.18; 'accepting': 0.18; 'library': 0.20; 'class,': 0.22; 'specified': 0.23; 'import': 0.24; 'patch': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'sense': 0.26; 'request,': 0.29; 'objects': 0.29; 'themselves': 0.29; 'skip:s 30': 0.31; 'knows': 0.32; 'statement': 0.32; 'class': 0.33; 'instance': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'skip:m 40': 0.36; 'totally': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'thought': 0.37; 'mean': 0.38; 'to:addr:python.org': 0.40; 'mark': 0.40; 'your': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'providing': 0.62; 'course': 0.62; 'is.': 0.63; 'thomas': 0.63; 'different': 0.63; 'intent': 0.66; 'python- list': 0.66; 'skip:\xe2 10': 0.70; 'unusual': 0.72; '_o__)': 0.84; 'assertion.': 0.84; 'received:125': 0.84; 'specialised': 0.84; 'subject:calls': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Mock object but also assert method calls?
Date Fri, 14 Aug 2015 07:21:54 +1000
References <509adc48-996d-44b0-abf1-e15ee387023b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:R/q/mT2l4ReNJmpz0eMcSmYowDA=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.178.1439500927.3627.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439500927 news.xs4all.nl 2881 [2001:888:2000:d::a6]:39476
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95358

Show key headers only | View raw


Thomas Lehmann via Python-list <python-list@python.org> writes:

> How about asserting that test2 of class Bar is called?

It is unusual to call class methods; do you mean a method on an
instance?

You will make a mock instance of the class, or a mock of the class; and
you'll need to know which it is.

> Of course I can do a patch for a concrete method but I was looking for
> something like:
>
>     mocked_object.assert_method_called_with(name="test2", "hello")

The ‘mock’ library defaults to providing ‘MagicMock’, which is “magic”
in the sense that it automatically provides any attribute you request,
and those attributes are themselves also MagicMocks::

    import unittest.mock

    def test_spam_calls_foo_bar():
        """ Should call the `spam` method on the specified `Foo` instance. """
        mock_foo = unittest.mock.MagicMock(system_under_test.Foo)
        system_under_test.spam(mock_foo)
        mock_foo.spam.assert_called_with("hello")

> 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. You should be instead using the specialised
methods from ‘unittest.TestCase’ and the methods on the mock objects
themselves.

-- 
 \       “It is the mark of an educated mind to be able to entertain a |
  `\                         thought without accepting it.” —Aristotle |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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