Python 如何使用test_客户端而不是客户端来测试烧瓶?
我用的是带烧瓶的Pytest夹具。我的应用程序是使用应用程序工厂实例化的Python 如何使用test_客户端而不是客户端来测试烧瓶?,python,python-3.x,flask,pytest,Python,Python 3.x,Flask,Pytest,我用的是带烧瓶的Pytest夹具。我的应用程序是使用应用程序工厂实例化的 #conftest.py @pytest.fixture(scope='session') def app(request): '''Session-wide test application''' app = create_app('testing') app.client = app.test_client() app_context = app.app_context() a
#conftest.py
@pytest.fixture(scope='session')
def app(request):
'''Session-wide test application'''
app = create_app('testing')
app.client = app.test_client()
app_context = app.app_context()
app_context.push()
def teardown():
app_context.pop()
request.addfinalizer(teardown)
return app
我想验证fixture创建的应用程序是否使用,因此我编写了一个测试:
#test_basics.py
def test_app_client_is_testing(app):
assert app.client() == app.test_client()
当我运行这个测试时,我得到:TypeError:“FlaskClient”对象不可调用
我做错了什么
测试是否不正确,或者夹具是否不正确?app.client已经是一个实例,您不应该再次调用它。最终,这个测试毫无意义。当然,客户机是一个测试客户机,这就是您刚才在fixture中创建它的方式。而且,客户端永远不会平等,它们是不同的实例
from flask.testing import FlaskClient
assert app.client == app.test_client() # different instances, never true
assert isinstance(app.client, app.test_client_class or FlaskClient) # still pointless, but correct
您可能需要两个装置:应用程序和客户端,而不是在应用程序上创建客户端
@pytest.yield_fixture
def app():
a = create_app('testing')
a.testing = True
with a.app_context():
yield a
@pytest.yield_fixture
def client(app):
with app.test_client() as c:
yield c
from flask.testing import FlaskClient
def test_app_client_is_client(app, client):
# why?
assert isinstance(client, app.test_client_class or FlaskClient)
app.client已经是一个实例,您不应该再次调用它。最终,这个测试毫无意义。当然,客户机是一个测试客户机,这就是您刚才在fixture中创建它的方式。而且,客户端永远不会平等,它们是不同的实例
from flask.testing import FlaskClient
assert app.client == app.test_client() # different instances, never true
assert isinstance(app.client, app.test_client_class or FlaskClient) # still pointless, but correct
您可能需要两个装置:应用程序和客户端,而不是在应用程序上创建客户端
@pytest.yield_fixture
def app():
a = create_app('testing')
a.testing = True
with a.app_context():
yield a
@pytest.yield_fixture
def client(app):
with app.test_client() as c:
yield c
from flask.testing import FlaskClient
def test_app_client_is_client(app, client):
# why?
assert isinstance(client, app.test_client_class or FlaskClient)