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


Groups > comp.lang.python > #42553

Re: python mock Requests and the response

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=797bd25ec=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.127
X-Spam-Level *
X-Spam-Evidence '*H*': 0.75; '*S*': 0.00; 'beginner': 0.05; 'url:voidspace': 0.09; 'python': 0.11; 'def': 0.12; 'lambda': 0.16; "response'": 0.16; 'typo': 0.16; 'subject:python': 0.16; 'url:)': 0.16; 'module': 0.19; 'trying': 0.19; 'passing': 0.19; "python's": 0.19; 'code,': 0.22; 'import': 0.22; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'installed': 0.27; 'to:2**1': 0.27; 'function': 0.29; 'url:mailman': 0.30; 'code': 0.31; 'requests': 0.31; 'values.': 0.31; 'class': 0.32; 'figure': 0.32; 'url:python': 0.33; '-----': 0.33; 'subject:the': 0.34; 'could': 0.34; 'basic': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'returning': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'step': 0.37; 'thank': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'url:mail': 0.40; 'tell': 0.60; 'hope': 0.61; 'further': 0.61; 'you.': 0.62; 'complete': 0.62; 'information': 0.63; 'received:194': 0.64; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; 'mock': 0.84; 'medium.': 0.91; 'subject:response': 0.91; 'url:latest': 0.91
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 2 Apr 2013 11:40:54 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To usmani kashif9957 <usmani.kashif9957@gmail.com>, python-list@python.org
In-Reply-To <46e77d09-ddb9-4430-8ac6-78f69cc24f51@googlegroups.com>
Subject Re: python mock Requests and the response
MIME-Version 1.0
X-Mailer Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.32.1364895662.17481.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364895662 news.xs4all.nl 6922 [2001:888:2000:d::a6]:50982
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42553

Show key headers only | View raw


----- Original Message -----
> I am a beginner to using mock in python and trying to use
> http://www.voidspace.org.uk/python/mock.
> 
> Please tell me the basic calls to get me working in below scenario. I
> am using python's Requests module
> (http://docs.python-requests.org/en/latest/) .
> 
> In my views.py, I have a function that makes variety of
> requests.get() calls with different response each time
> 
>     def myview(request):
>       res1 = requests.get('aurl')
>       res2 = request.get('burl')
>       res3 = request.get('curl')
> 
> In my test class I want to do something like this but cannot figure
> out exact method calls
> 
> Step 1:
> 
>     //Mock the requests module
>     //when mockedRequests.get('aurl') is called then return 'a
>     response'
>     //when mockedRequests.get('burl') is called then return 'b
>     response'
>     //when mockedRequests.get('curl') is called then return 'C
>     response'
> 
> Step 2:
> 
> Call my view
> 
> Step 3:
> 
> verify response contains 'a response', 'b response' , 'c response'
> 
> Please help me to complete Step 1.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Hi,

Why are you passing the requests (with typo ?) module to the function ? anyway, the code could look like this:

!! untested code, I don't have the requests module installed !!

import requests
import mock

def myView():
	res1 = requests.get('aurl')
	res2 = requests.get('burl')
	res3 = requests.get('curl')

@mock.patch('requests.get', mock.Mock(side_effect = lambda k:{'aurl': 'a response', 'burl' : 'b response'}.get(k, 'unhandled request %s'%k)))
def testMyView(mockedGet):
	# you can further customize the mock object here
	mockedGet.whatever()
	myView()

testMyView()


side_effect is the mock way of returning multiple values.

Hope it helps,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: python mock Requests and the response Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-04-02 11:40 +0200

csiph-web