pytest-dev / pytest-flask

A set of pytest fixtures to test Flask applications

Home Page:http://pytest-flask.readthedocs.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using live_server for Flask web app tests inside docker container

gqoew opened this issue · comments

commented

I want to test the following Flask app inside a docker container. The default flask port 5000 is binded (docker run -p 5000:5000):

from flask import Flask

def create_app():
    app = Flask(__name__)
    return app

app = create_app()

@app.route('/')
def index():
    return render_template('index.html')

My test is as follows:

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

@pytest.mark.usefixtures('live_server')
class TestLiveServer:

    def test_homepage(self):
        res = urlopen('http://0.0.0.0:5000')
        assert b'OK' in res.read()
        assert res.code == 200

I get urllib.error.URLError: <urlopen error [Errno 111] Connection refused>.

Should I change my host + port config somewhere?

Hi @gqoew!

Where is your test located and how are you launching it?

@gqoew,

please use url_for to get endpoint for your scenario as per documentation

commented

Hi @gqoew!

Where is your test located and how are you launching it?

My app.py file is at the root of my repo, my test file is under a /tests directory. I launch the tests with pytest tests/test_app.py.

I use pytest-flask==0.12.0 and I am running all this inside a container based on the official python docker image.

@gqoew,

please use url_for to get endpoint for your scenario as per documentation

When I use urlopen(url_for('index', _external=True)), I get the error:

werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you mean 'static' instead?

Hey @gqoew, at first glance this seems to be more of a docker issue rather than something related to this plugin. Also, more details regarding your environment would be necessary to debug this. I'll be closing the issue for the time being, but feel free to reopen it to provide a minimal, reproducible example.