Testing a Flask app with pytest-flask + pytest-selenium (docker)
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 pytestimport requestsfrom 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?
The referenced pytest-selenium issue has:
@pytest.fixturedef 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)
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=:99pytest --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.