我正在尝试测试一个在一个对接容器中的一个水瓶座网络应用程序,这对我来说是新的。我的堆栈如下:
这是我的烧瓶应用程序文件:
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:消息: Service /usr/local/bin/firefox意外退出。状态代码为:1错误:未指定显示环境变量。
我能够运行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:
是否需要以不同的方式引导实时服务器?还是应该在某个地方更改主机+端口配置?
发布于 2018-10-11 18:41:39
关于这个问题,直接调用urllib:
Pytest的活动服务器默认使用随机端口。可以将此参数添加到pytest调用中:
--live-server-port 5000
如果没有此参数,则可以直接调用活动服务器,例如:
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我想您有一个名为index的视图函数。它将自动添加正确的端口号。
但这和码头没什么关系,你是怎么操作的?
关于Selenium本身的问题,我可以想象码头网络相关的问题。你是怎么用的?你拿到了吗?docker-compose配置?你能分享一下吗?
发布于 2018-09-03 17:03:10
引用的pytest-selenium问题有:
@pytest.fixture
def firefox_options(firefox_options, pytestconfig):
if pytestconfig.getoption('headless'):
firefox_options.add_argument('-headless')
return firefox_options注意- (单破折号)在headless之前的add_argument()
(来源)
发布于 2020-07-21 12:32:52
对于后来者来说,看一看Xvfb也许是值得的,更有帮助的是这个教程。
然后(在中)您可以输入:
Xvfb :99 &
export DISPLAY=:99
pytest --driver Firefox --driver-path /usr/local/bin/firefox test_app.py这为应用程序提供了一个虚拟帧缓冲区(假屏幕),并且它输出了所有的图形内容。
请注意,我没有遇到这个问题,只是提供了一个解决方案,帮助我克服了另一个应用程序中提到的错误。
https://stackoverflow.com/questions/52153076
复制相似问题