我有一个简单的flask应用程序,我想使用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
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,并更新了pytest和pytest-flask.但没有一个工作.
我能够通过以下方式通过测试:
>从虚拟环境中删除了pytest和pytest-flask.
>删除了它们在我的系统范围内的安装.
>奇怪的是,我有一个名为flask-pytest的软件包,我将其删除了(在环境中)
>在系统范围内再次安装它们.
>将它们再次安装在virtualenvironment上.
我不知道这对测试有什么影响,但是它有效.唯一不同的是,我没有安装所说的烧瓶最热的东西.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。