Testing a Flask app with pytest-flask + pytest-selenium (docker)

3.1k Views Asked by At

I am trying to test a Flask web app within a docker container, which is new for me. My stack is the following:

  • firefox
  • selenium
  • pytest-selenium
  • pytest-flask

Here is my Flask app file:

from flask import Flask

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

app = create_app()

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

Now, my test file which verifies the title of my index page:

import pytest
from app import create_app

# from https://github.com/pytest-dev/pytest-selenium/issues/135
@pytest.fixture
def firefox_options(request, firefox_options):
    firefox_options.add_argument('--headless')
    return firefox_options

# from https://pytest-flask.readthedocs.io/en/latest/tutorial.html#step-2-configure
@pytest.fixture
def app():
    app = create_app()
    return app

# from https://pytest-flask.readthedocs.io/en/latest/features.html#start-live-server-start-live-server-automatically-default
@pytest.mark.usefixtures('live_server')
class TestLiveServer:

    def test_homepage(self, selenium):
        selenium.get('http://0.0.0.0:5000')
        h1 = selenium.find_element_by_tag_name('h1')
        assert h1 == 'title'

When I run my tests with:

pytest --driver Firefox --driver-path /usr/local/bin/firefox test_app.py

I get the following error (which seems due to firefox not in headless mode).

selenium.common.exceptions.WebDriverException: Message: Service /usr/local/bin/firefox unexpectedly exited. Status code was: 1
Error: no DISPLAY environment variable specified

I am able to run firefox --headless but it seems my pytest fixture didn't manage to do the setup. Is there a better way to do this?

Now, if I replace selenium.get() by urlopen just to try the correct initialization of the app and its connection:

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

I get the error:

urllib.error.URLError:

Do I need to boot the live server differently? Or should I change my host + port config somewhere?

3

There are 3 best solutions below

0
On

For late comers, it might be worthwhile taking a look at Xvfb and even more helpful can be this tutorial

Then (in Linux shell) you can enter:

Xvfb :99 &
export DISPLAY=:99
pytest --driver Firefox --driver-path /usr/local/bin/firefox test_app.py

This provides a virtual frame buffer (fake screen) for the application and it outputs all the graphical content there.

Note that I did not encountered this problem, just providing a solution that helped me overcome the mentioned error with an another app.

1
On

The referenced pytest-selenium issue has:

@pytest.fixture
def firefox_options(firefox_options, pytestconfig):
    if pytestconfig.getoption('headless'):
        firefox_options.add_argument('-headless')
    return firefox_options

Note the - (single dash) preceding headless in add_argument()

(Source)

0
On

Regarding the problem with a direct call with urllib:

Pytest's live server uses random port by default. You can add this parameter to pytest invocation:

--live-server-port 5000

Or without this parameter you can make direct calls to live server like:

import pytest
import requests

from flask import url_for


@pytest.mark.usefixtures('live_server')
def test_something():
    r = requests.get(url_for('index', _external=True))
    assert r.status_code == 200

I suppose you have view function called index. It would add a correct port number automatically.

But this doesn't have to do anything with docker, how do you run it?

Regarding the problem with Selenium itself - I can imagine docker networks related problem. How do you use it? Do you have eg. docker-compose configuration? Can you share it?