如何从烧瓶test_client获取负载?

时间:2020-02-24 16:07:52

标签: testing flask flask-restplus

如何在烧瓶post上使用test_client发送有效载荷?

具有test_client

的灯具
@pytest.fixture
def test_client():
    app = create_app(configuration_name="testing")
    return app.test_client()

我要发送的有效载荷

@pytest.fixture
def selection_payload():
    return {
        'name': 'random-selection-name',
        'description': 'mock-selection-description',
        'products_skus': ['123', '456']
    }

测试:

def test_can_create_selection(self, test_client, selection_payload):
    response = test_client.post(
        self.ENDPOINT,
        data=selection_payload
    )

    assert response.status_code == HTTPStatus.CREATED

当我尝试使用api.payload访问有效负载时,值为None,我也尝试从request.get_json()导入flask

def post(self):
    try:
        selection = Selection(**api.payload) # api.payload is None
        result = selection.save()
    except NotUniqueError:
        api.abort(HTTPStatus.CONFLICT, 'code already exist.')

    return result

0 个答案:

没有答案