[英]test flask app using pytest can't find fixture client
I have a simple flask app, i want to test it using pytest.我有一个简单的烧瓶应用程序,我想使用 pytest 对其进行测试。
my conftest.py
:我的
conftest.py
:
@pytest.fixture
def app(self):
app = create_app(TestingConfig)
return app
my test_view.py
:我的
test_view.py
:
class TestMainView:
def test_ping(self, client):
res = client.get(url_for('main.index'))
assert res.status_code == 200
when i run the test's using pytest
it's throwing an error saying:当我使用
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.
i have another testing file where i test default configs and it passes:我有另一个测试文件,我在其中测试默认配置并通过:
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'
edit : i was able to pass an empty test by changing to:编辑:我能够通过更改为:
conftest.py
: conftest.py
:
@pytest.fixture
def app():
app = create_app(TestingConfig)
return app
@pytest.fixture
def client(app):
return app.test_client()
and my test file to:和我的测试文件:
def test_index(client):
assert True
but, still i can't pass the test_index
if it was:但是,如果是这样,我仍然无法通过
test_index
:
def test_index(client):
assert client.get(url_for('main.index')).status_code == 200
but this time, I'm getting an error stating that says:但这一次,我收到一条错误消息,指出:
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
i tried so many different things.我尝试了很多不同的东西。 i updated
pip
for this project's virtualenvironment
and updated the pytest
and pytest-flask
.我更新
pip
为这个项目的virtualenvironment
,并更新了pytest
和pytest-flask
。 but none did work.但没有一个工作。
I was able to pass the tests by:我能够通过以下测试:
pytest
and pytest-flask
from virtualenvironment
.virtualenvironment
pytest-flask
删除了pytest
和pytest-flask
。flask-pytest
i removed it(in the env
)flask-pytest
的包我删除了它(在env
)virtualenvironment
.virtualenvironment
再次安装它们。 i don't know how this had anything with the tests, but it worked.我不知道这与测试有什么关系,但它有效。 the only thing different is that i didn't installed the said
flask-pytest
thing.唯一不同的是我没有安装所说的
flask-pytest
东西。
You need to delete your incorrect client fixture
(see the implementation of pytest-flask
for the correct one).您需要删除不正确的
client fixture
(请参阅pytest-flask
的实现以获得正确的)。
Afterwards you need to install pytest-flask
and pytest
inside of the virtualenv
, you better remove the system wide ones to avoid confusion.之后您需要在
virtualenv
pytest-flask
安装pytest-flask
和pytest
,您最好删除系统范围内的以避免混淆。
Afterwards you should be able to run your tests.之后,您应该能够运行您的测试。
I just had the same issue.我只是有同样的问题。 The solution was:
解决方案是:
pip uninstall pytest-flask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.