Re: [flask] Testing with python-requests?
- From:
- Ron DuPlain
- Date:
- 2012-01-07 @ 16:29
Hi,
On Sat, Jan 7, 2012 at 11:17 AM, Rene Weiss <rmweiss@nullmodem.ch> wrote:
> Is it possible to use requests (python-requests.org) in
> the Unit-Tests?
>
> I know that there are the build-in functions and twill in
> flask-testing, but i would like to use the same library
> that i use at other places.
>
> I tried it with the following code:
>
> ---
>
> def test_startpage(self):
> rv = self.client.get('/')
> self.assertIn('test', rv.data)
This example is working at the WSGI level, where no http server is
running on localhost and therefore no port is used.
> def test_startpage_twill(self):
> with Twill(self.app, port=5000) as t:
> t.browser.go(t.url("/"))
> self.assertIn('test', t.browser.get_html())
Looks like Twill is running a simple httpd on port 5000.
> def test_startpage_requests(self):
> r = requests.get('http://127.0.0.1:5000/')
> self.assertIn('>test', r.content)
Nothing is running on port 5000 here. Requests is just a client
library. You'll need to run the server on port 5000. Perhaps there
are recommended recipes for doing this with Requests (I don't know),
but you could always put this in a Twill with block, i.e. use the
second example with block and the third example for http requests.
No matter the solution, you need a web server listening at port 5000
(or whatever port you choose).
-Ron
> ---
>
> The first two examples worked, but the third returns
> "error(111, 'Connection refused')"
>
> Maybe i just use the wrong port, but i have not found
> any information about whether flask use another port
> for testing.
>
>
> Thanks,
>
> René Weiss
>