Mat*_*ain 2 python pytest web-api-testing
我正在为 post api 编写测试,它返回创建的资源。但是如何将这些数据传递给 python 中的夹具,以便在测试完成后进行清理
清理:
@pytest.fixture(scope='function')
def delete_after_post(request):
def cleanup():
// Get ID of resource to cleanup
// Call Delete api with ID to delete the resource
request.addfinalizer(cleanup)
Run Code Online (Sandbox Code Playgroud)
测试:
def test_post(delete_after_post):
Id = post(api)
assert Id
Run Code Online (Sandbox Code Playgroud)
将响应(ID)传递回夹具以进行清理的最佳方法是什么。不想将清理作为测试的一部分。
您可以使用请求实例访问该 ID,并通过request.instance.variableName. 就像,假设您删除 id 的方法delete(resource_id),在这里
conftest.py
import pytest
@pytest.fixture(scope='function')
def delete_after_post(request):
def cleanup():
print request.node.resourceId
# Get ID of resource using request.instance.resourceId
# Call Delete api with ID to delete the resource
request.addfinalizer(cleanup)
Run Code Online (Sandbox Code Playgroud)
测试文件 xyz_test.py
def test_post(delete_after_post,request):
request.node.resourceId='3'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3427 次 |
| 最近记录: |