Skip to content

HTTP request using 'live_server' #45

@jeff1evesque

Description

@jeff1evesque

I took a live_server example, and placed it on the root of my project (not committed), as manager.py:

# @manager.py

import urllib2
from flask import Flask, url_for
import pytest

def test_add_endpoint_to_live_server(live_server):
    @live_server.app.route('/load-data/')
    def test_endpoint():
        return 'got it', 200

    live_server.start()

    res = urllib2.urlopen(url_for('test_endpoint', _external=True))
    assert res.code == 200

Then, defined a very simple conftest.py, also on the root of my project (not committed):

#confteset.py

from factory import create_app

@pytest.fixture
def app():
    app = create_app()
    return app

My pytest.ini, is pretty simple as well, and again on the root of my project (not committed):

[pytest]
python_files=manager.py
python_classes=Test
python_functions=test

I've attempted to implement py.test several different ways in terminal (vagrant vm):

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ cd /vagrant
vagrant@vagrant-ubuntu-trusty-64:/vagrant$ py.test
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config.py", line 320, in
_importconftest
    mod = conftestpath.pyimport()
  File "/usr/local/lib/python2.7/dist-packages/py/_path/local.py", line 650, in
pyimport
    __import__(modname)
  File "/vagrant/conftest.py", line 17, in <module>
    from factory import create_app
  File "/vagrant/factory.py", line 20, in <module>
    from interface.views import blueprint
  File "/vagrant/interface/views.py", line 10, in <module>
    from brain.load_data import Load_Data
ImportError: No module named brain.load_data
ERROR: could not load /vagrant/conftest.py

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ python -m pytest manager.py
============================= test session starts ==============================

platform linux2 -- Python 2.7.6, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /vagrant, inifile: pytest.ini
plugins: flask-0.10.0
collected 1 items

manager.py F

=================================== FAILURES ===================================

_______________________ test_add_endpoint_to_live_server _______________________


live_server = <LiveServer listening at http://localhost:51685>

    def test_add_endpoint_to_live_server(live_server):
        @live_server.app.route('/load-data/')
        def test_endpoint():
            return 'got it', 200

        live_server.start()

>       res = urllib2.urlopen(url_for('test_endpoint', _external=True))

manager.py:30:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

/usr/lib/python2.7/urllib2.py:127: in urlopen
    return _opener.open(url, data, timeout)
/usr/lib/python2.7/urllib2.py:410: in open
    response = meth(req, response)
/usr/lib/python2.7/urllib2.py:523: in http_response
    'http', request, response, code, msg, hdrs)
/usr/lib/python2.7/urllib2.py:448: in error
    return self._call_chain(*args)
/usr/lib/python2.7/urllib2.py:382: in _call_chain
    result = func(*args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


self = <urllib2.HTTPDefaultErrorHandler instance at 0x7f4e9eb567e8>
req = <urllib2.Request instance at 0x7f4e9eb6d290>
fp = <addinfourl at 139975646958944 whose fp = <socket._fileobject object at 0x7
f4e9eb92dd0>>
code = 405, msg = 'METHOD NOT ALLOWED'
hdrs = <httplib.HTTPMessage instance at 0x7f4e9eb6d518>

    def http_error_default(self, req, fp, code, msg, hdrs):
>       raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
E       HTTPError: HTTP Error 405: METHOD NOT ALLOWED

/usr/lib/python2.7/urllib2.py:531: HTTPError
----------------------------- Captured stderr call -----------------------------

Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pytest_flask/fixtures.py", line 5
9, in <lambda>
    worker = lambda app, port: app.run(port=port, use_reloader=False)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 843, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 694, i
n run_simple
    inner()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 656, i
n inner
    fd=fd)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 550, i
n make_server
    passthrough_errors, ssl_context, fd=fd)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 464, i
n __init__
    HTTPServer.__init__(self, (host, int(port)), handler)
  File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
    self.server_bind()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
    self.socket.bind(self.server_address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
=========================== 1 failed in 2.27 seconds ===========================

I spent this past weekend, and a few days last week after work, trying to understand pytest. I noticed pytest-flask, and really like the rst files being generated to the documentation. But, I haven't been successful implementing the above example taken straight out from the live_server documentation. My overall goal, is to have pytest-flask start a flask instance (app context?). This flask instance (i.e. http://localhost:xxxx), would exist for the duration, or scope of the unit testing. Specifically, I need the flask instance to be running, and I want to run some of my general programmatic_interface tests. Later, I will drill down, and have more specific unit tests.

I've been going back and forth, trying to implement live_server, and client.post(). However, the documentation very lightly discusses about client.get(), and never mentions the existence of client.post(). I was hoping if I used live_server, I would have a flask instance (i.e. http://localhost:xxxx), which would exist for the duration of the implemented unit tests. But, I wasn't sure if I need to cleverly integrate client.post() within the live_test implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions