[TIP] mock object not being used?
Christine Jones
cjjones at pixar.com
Wed Sep 18 19:58:29 PDT 2013
Thank you for responding Michael and Marius to my initial question. I
tried both methods below Michael but it seems my mock object is not
being used in a either function. Perhaps my assumptions of how mock
should work are incorrect. Looking at the first test (updated with your
fix) and the function in my view, I assume that I can replace the way
'players' is set in my function with the 'players' value in my test.
Then when I set 'r' in my test it calls the signups function and runs
through the function using my mock object. However, my assert statement
fails. What am I missing or misunderstanding. Thanks for all the help.
Test for if branch:
======================
# READ - GET signups - 200 (assumes > 0 signups)
def test_signups_get(self):
today = datetime.datetime.now()
from lunchlottoweb import models
with patch.object(models, "LottoPlayer") as MockLottoPlayer:
players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
NonCallableMock(employeelogin='dlovrn', employeedept='systems', signupdate=today, lottowinner=False)]
MockLottoPlayer.all = players
request = MagicMock(name='signups_multiple_exist')
r = self.app.get('/signups')
assert 'cjjones' in r.data
My function:
======================
# admin page: today's signups
@app.route('/signups', methods=['GET'])
def signups():
today = date.today()
players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) == today).distinct(LottoPlayer.employeelogin).all()
player_count = players.__len__()
if player_count > 1:
return render_template("signups_today.html", signups=players)
else:
return render_template("signups_today.html")
On 09/16/2013 12:20 PM, testing-in-python-request at lists.idyll.org wrote:
> Send testing-in-python mailing list submissions to
> testing-in-python at lists.idyll.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.idyll.org/listinfo/testing-in-python
> or, via email, send a message with subject or body 'help' to
> testing-in-python-request at lists.idyll.org
>
> You can reach the person managing the list at
> testing-in-python-owner at lists.idyll.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of testing-in-python digest..."
>
>
> Today's Topics:
>
> 1. Re: unittest/mock question (Michael Foord)
> 2. Re: unittest/mock question (Marius Gedminas)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 16 Sep 2013 11:28:18 +0100
> From: Michael Foord <michael at voidspace.org.uk>
> Subject: Re: [TIP] unittest/mock question
> To: Christine Jones <cjjones at pixar.com>
> Cc: testing-in-python at lists.idyll.org
> Message-ID: <E6D76F0F-A6F0-4097-970F-7A01B3C8B3D2 at voidspace.org.uk>
> Content-Type: text/plain; charset=us-ascii
>
>
> On 14 Sep 2013, at 00:26, Christine Jones <cjjones at pixar.com> wrote:
>
>> Hello,
>> I've just started using Python unittest and mock. I've gone through the three Michael Foord tutorials, and read the docs, however, I cannot figure my way around this issue.
>>
>> I am using Python 2.6 and mock 1.0.1.
>>
>> I have the following function and tests. In brief, I am not able to test both branches of my if/else statement unless I comment out one or the other tests. Specifically, if I run both tests below only the last one passes. However, if I comment out the last one, the first one runs and passes.
>>
>> I was under the impression that patch automatically unmonkey patches when the context or function is left but it seems like maybe this is not happening. Or I am missing some other key concept. Any help would be greatly appreciated. Thank you in advance!
>> Christine
>>
>> My function:
>> ======================
>> # admin page: today's signups
>> @app.route('/signups', methods=['GET'])
>> def signups():
>> today = date.today()
>> players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) == today).distinct(LottoPlayer.employeelogin).all()
>> player_count = players.__len__()
>> if player_count > 1:
>> return render_template("signups_today.html", signups=players)
>> else:
>> return render_template("signups_today.html")
>>
>> Test for if branch:
>> ======================
>> # READ - GET signups - 200 (assumes > 0 signups)
>> def test_signups_get(self):
>> today = datetime.datetime.now()
>> from lunchlottoweb import models
>> with patch.object(models, "LottoPlayer"):
>> players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
>> NonCallableMock(employeelogin='dlovirin', employeedept='systems', signupdate=today, lottowinner=False)]
>> LottoPlayer.all = players
>> request = MagicMock(name='signups_multiple_exist')
>> r = self.app.get('/signups')
>> assert 'cjjones' in r.data
>>
>> Test for else branch:
>> ======================
>> # READ - GET signups when there are 0 signups
>> def test_signups_get_when_none(self):
>> now = datetime.datetime.now()
>> yesterday = now - datetime.timedelta(days = 1)
>> from lunchlottoweb import models
>> with patch.object(models, 'LottoPlayer'):
>> players = [NonCallableMock(employeelogin='jshmo', employeedept='systems', signupdate=yesterday, lottowinner=False),
>> NonCallableMock(employeelogin='foo', employeedept='systems', signupdate=yesterday, lottowinner=False)]
>> LottoPlayer.all = players
>>
>
> You're patching models.LottoPlayer - but directly modifying the LottoPlayer object (which is *not* your patched version).
>
> Change LottoPlayer.all in your code above to models.LottoPlayer, *or* do the following, to fix your problem:
>
> with patch.object(models, 'LottoPlayer') as MockLottoPlayer:
> ...
> MockLottoPlayer.all = players
>
> Michael
>
>> request = MagicMock(name='signupsrequest')
>> r = self.app.get('/signups')
>> assert 'No one has signed up yet' in r.data
>>
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python
>
> --
> http://www.voidspace.org.uk/
>
>
> May you do good and not evil
> May you find forgiveness for yourself and forgive others
> May you share freely, never taking more than you give.
> -- the sqlite blessing
> http://www.sqlite.org/different.html
>
>
>
>
>
>
>
>
> ------------------------------
>
> Message: 2
> Date: Mon, 16 Sep 2013 13:58:03 +0300
> From: Marius Gedminas <marius at gedmin.as>
> Subject: Re: [TIP] unittest/mock question
> To: testing-in-python at lists.idyll.org
> Message-ID: <20130916105803.GB11184 at platonas>
> Content-Type: text/plain; charset="us-ascii"
>
> On Fri, Sep 13, 2013 at 04:26:25PM -0700, Christine Jones wrote:
>> I've just started using Python unittest and mock. I've gone through
>> the three Michael Foord tutorials, and read the docs, however, I
>> cannot figure my way around this issue.
>>
>> I am using Python 2.6 and mock 1.0.1.
>>
>> I have the following function and tests. In brief, I am not able to
>> test both branches of my if/else statement unless I comment out one or
>> the other tests. Specifically, if I run both tests below only the last
>> one passes. However, if I comment out the last one, the first one runs
>> and passes.
>>
>> I was under the impression that patch automatically unmonkey patches
>> when the context or function is left but it seems like maybe this is
>> not happening. Or I am missing some other key concept. Any help would
>> be greatly appreciated. Thank you in advance!
>>
>> Christine
>>
>> My function:
>> ======================
>> # admin page: today's signups
>> @app.route('/signups', methods=['GET'])
>> def signups():
>> today = date.today()
>> players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) == today).distinct(LottoPlayer.employeelogin).all()
> What ORM is this? It may matter.
>
>> player_count = players.__len__()
> That's an unusual spelling of
>
> player_count = len(players)
>
>> if player_count > 1:
>> return render_template("signups_today.html", signups=players)
>> else:
>> return render_template("signups_today.html")
>>
>> Test for if branch:
>> ======================
>> # READ - GET signups - 200 (assumes > 0 signups)
>> def test_signups_get(self):
>> today = datetime.datetime.now()
>> from lunchlottoweb import models
>> with patch.object(models, "LottoPlayer"):
> I think you're supposed to do
>
> with patch.object(models, "LottoPlayer") as LottoPlayer:
>
> here. Not 100% sure.
>
>> players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
>> NonCallableMock(employeelogin='dlovirin', employeedept='systems', signupdate=today, lottowinner=False)]
>> LottoPlayer.all = players
> Because if you don't, then you're modifying some global object, but I
> don't see where it came from.
>
>> request = MagicMock(name='signups_multiple_exist')
>> r = self.app.get('/signups')
>> assert 'cjjones' in r.data
>>
>> Test for else branch:
>> ======================
>> # READ - GET signups when there are 0 signups
>> def test_signups_get_when_none(self):
>> now = datetime.datetime.now()
>> yesterday = now - datetime.timedelta(days = 1)
>> from lunchlottoweb import models
>> with patch.object(models, 'LottoPlayer'):
> Same here
>
>> players = [NonCallableMock(employeelogin='jshmo', employeedept='systems', signupdate=yesterday, lottowinner=False),
>> NonCallableMock(employeelogin='foo', employeedept='systems', signupdate=yesterday, lottowinner=False)]
>> LottoPlayer.all = players
> But then again at the moment you're modifying the same global object
> from both tests, so I don't quite understand what is happening...
>
> Perhaps the ORM layer caches something?
>
>> request = MagicMock(name='signupsrequest')
>> r = self.app.get('/signups')
>> assert 'No one has signed up yet' in r.data
> Marius Gedminas
--
Christine
P I X A R
More information about the testing-in-python
mailing list