[英]test flask app using pytest can't find fixture client
我有一个简单的烧瓶应用程序,我想使用 pytest 对其进行测试。
我的conftest.py
:
@pytest.fixture
def app(self):
app = create_app(TestingConfig)
return app
我的test_view.py
:
class TestMainView:
def test_ping(self, client):
res = client.get(url_for('main.index'))
assert res.status_code == 200
当我使用pytest
运行测试时,它抛出一个错误说:
fixture 'client' not found
> available fixtures: app, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
我有另一个测试文件,我在其中测试默认配置并通过:
class TestTestingClass(object):
app = create_app(TestingConfig)
def test_app_is_test(self):
''' test the test enviroment is set okay and works'''
assert self.app.config['SECRET_KEY'] == 'something hard'
assert self.app.config['DEBUG'] == True
assert self.app.config['TESTING'] == True
assert current_app is not None
assert self.app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:////tmp/testing.db'
编辑:我能够通过更改为:
conftest.py
:
@pytest.fixture
def app():
app = create_app(TestingConfig)
return app
@pytest.fixture
def client(app):
return app.test_client()
和我的测试文件:
def test_index(client):
assert True
但是,如果是这样,我仍然无法通过test_index
:
def test_index(client):
assert client.get(url_for('main.index')).status_code == 200
但这一次,我收到一条错误消息,指出:
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
我尝试了很多不同的东西。 我更新pip
为这个项目的virtualenvironment
,并更新了pytest
和pytest-flask
。 但没有一个工作。
我能够通过以下测试:
virtualenvironment
pytest-flask
删除了pytest
和pytest-flask
。flask-pytest
的包我删除了它(在env
)virtualenvironment
再次安装它们。 我不知道这与测试有什么关系,但它有效。 唯一不同的是我没有安装所说的flask-pytest
东西。
您需要删除不正确的client fixture
(请参阅pytest-flask
的实现以获得正确的)。
之后您需要在virtualenv
pytest-flask
安装pytest-flask
和pytest
,您最好删除系统范围内的以避免混淆。
之后,您应该能够运行您的测试。
我只是有同样的问题。 解决方案是:
pip uninstall pytest-flask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.