【问题标题】:test is failing because flask is returning stream insted of json测试失败,因为烧瓶正在返回流而不是 json
【发布时间】:2021-01-06 16:07:41
【问题描述】:

我是我的烧瓶应用程序,我有一个路由/控制器来创建我称之为实体的东西:

@api.route('/entities', methods=['POST'])
def create_entity():
    label = request.get_json()['label']
    entity = entity_context.create(label)
    print('the result of creating the entity: ')
    print(entity)
    return entity

创建实体后,打印以下内容:

the result of creating the entity: 
{'label': 'Uganda', '_id': '{"$oid": "5ff5df24bb80fcf812631c53"}'}

我为此控制器编写了以下测试:

@given("an entity is created with label Uganda", target_fixture="create_entity")
def create_entity(test_client):
    result = test_client.post('api/entities', json={"label": "Uganda"})
    return result

@then("this entity can be read from the db")
def get_entities(create_entity, test_client):
    print('result of create entity: ')
    print(create_entity)
    response = test_client.get('api/entities').get_json()
    assert create_entity['_id'] in list(map(lambda x : x._id, response))

测试客户端在conftest.py中定义如下:

@pytest.fixture
def test_client():
    flask_app = init_app()
    # Create a test client using the Flask application configured for testing
    with flask_app.test_client() as flask_test_client:
        return flask_test_client

我的测试失败并出现以下错误:

create_entity = <Response streamed [200 OK]>, test_client = <FlaskClient <Flask 'api'>>

    @then("this entity can be read from the db")
    def get_entities(create_entity, test_client):
        print('result of create entity: ')
        print(create_entity)
        response = test_client.get('api/entities').get_json()
>       assert create_entity['_id'] in list(map(lambda x : x._id, response))
E       TypeError: 'Response' object is not subscriptable

the result of creating the entity: 
{'label': 'Uganda', '_id': '{"$oid": "5ff5df24bb80fcf812631c53"}'}
result of create entity: 
<Response streamed [200 OK]>.      

显然create_entity 不是返回一个简单的对象,而是一个流式响应:

<Response streamed [200 OK]>

如果控制器返回 json,我不明白为什么这不会返回简单的 json?

【问题讨论】:

    标签: python flask pytest fixtures pytest-bdd


    【解决方案1】:

    你的测试有一些问题

    首先你不是在烧瓶测试上下文中执行:

    @pytest.fixture
    def test_client():
        flask_app = init_app()
        # Create a test client using the Flask application configured for testing
        with flask_app.test_client() as flask_test_client:
            return flask_test_client
    

    这必须是yield flask_test_client -- 否则上下文管理器在您的测试运行时已经退出

    其次,您将获得 create_entityResponse 对象,因为这就是您的 @given 夹具返回的内容:

    @given("an entity is created with label Uganda", target_fixture="create_entity")
    def create_entity(test_client):
        result = test_client.post('api/entities', json={"label": "Uganda"})
        return result
    

    .post(...) 的结果是一个 Response 对象,如果你希望它成为 json 的字典,你需要在其上调用 .get_json()

    【讨论】:

      猜你喜欢
      • 2019-01-06
      • 2017-01-11
      • 2019-05-03
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多