Python 使用pytest罐测试烧瓶app';找不到fixture客户端

Python 使用pytest罐测试烧瓶app';找不到fixture客户端,python,flask,pytest,Python,Flask,Pytest,我有一个简单的烧瓶应用程序,我想用pytest测试它 myconftest.py: @pytest.fixture def app(self): app = create_app(TestingConfig) return app class TestMainView: def test_ping(self, client): res = client.get(url_for('main.index')) assert res.status_code ==

我有一个简单的烧瓶应用程序,我想用pytest测试它

my
conftest.py

@pytest.fixture
def app(self):
    app = create_app(TestingConfig)

    return app
class TestMainView:

def test_ping(self, client):
    res = client.get(url_for('main.index'))
    assert res.status_code == 200
@pytest.fixture
def app():
    app = create_app(TestingConfig)

    return app


@pytest.fixture
def client(app):
    return app.test_client()
我的
测试视图.py

@pytest.fixture
def app(self):
    app = create_app(TestingConfig)

    return app
class TestMainView:

def test_ping(self, client):
    res = client.get(url_for('main.index'))
    assert res.status_code == 200
@pytest.fixture
def app():
    app = create_app(TestingConfig)

    return app


@pytest.fixture
def client(app):
    return app.test_client()
当我使用
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(self):
    app = create_app(TestingConfig)

    return app
class TestMainView:

def test_ping(self, client):
    res = client.get(url_for('main.index'))
    assert res.status_code == 200
@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
但是,如果它是:

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.

我尝试了很多不同的东西。我更新了该项目的
virtualenvironment
pip
,并更新了
pytest
pytest烧瓶
。但没有一个起作用

我能够通过以下测试:

  • virtualenvironment
    中删除
    pytest
    pytest烧瓶
  • 删除了系统范围内的安装
  • 奇怪的是,我有一个名为
    flask pytest
    的包,我删除了它(在
    env
    中)
  • 在系统范围内重新安装它们
  • virtualenvironment
    上再次安装它们

我不知道这和测试有什么关系,但它起作用了。唯一不同的是,我没有安装所说的
pytest
东西。

您需要删除不正确的
客户端设备
(有关正确的设备,请参阅
pytest flask
的实现)

之后,您需要在
virtualenv
内部安装
pytest flask
pytest
,最好删除系统范围内的烧瓶,以避免混淆


之后,您应该能够运行测试。

我也遇到了同样的问题。解决办法是:

pip uninstall pytest-flask

客户端夹具用于第二种类型的测试和实现。你说的是我提供的第一种类型的代码,但那不起作用。我仍在试验,可能会根据我为修复它所做的事情来更改解决方案。问题是烧瓶pytest和pytest烧瓶之间的混淆。这是正确的选择。