Я пытаюсь протестировать веб-приложение Flask в док-контейнере, что для меня ново. Мой стек следующий:
- светлячок
- Селен
- pytest-Селен
- pytest-колба
Вот мой файл приложения Flask:
from flask import Flask
def create_app():
app = Flask(__name__)
return app
app = create_app()
@app.route('/')
def index():
return render_template('index.html')
Теперь мой тестовый файл, который проверяет заголовок моей индексной страницы:
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'
Когда я запускаю свои тесты с:
pytest --driver Firefox --driver-path /usr/local/bin/firefox test_app.py
Я получаю следующую ошибку (которая возникает из-за того, что firefox не находится в режиме без головы)
selenium.common.exceptions.WebDriverException: Message: Service /usr/local/bin/firefox unexpectedly exited. Status code was: 1
Error: no DISPLAY environment variable specified
Я могу запустить firefox --headless
, но, похоже, моему устройству pytest не удалось выполнить настройку. Есть ли лучший способ сделать это?
Теперь, если я заменю selenium.get()
на urlopen
, просто попробуйте правильно инициализировать приложение и его соединение:
def test_homepage(self):
res = urlopen('http://0.0.0.0:5000')
assert b'OK' in res.read()
assert res.code == 200
Я получаю ошибку:
urllib.error.URLError:
Нужно ли загружать живой сервер по-другому? Или я должен где-нибудь изменить свой хост + порт конфигурации?