如何在烧瓶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