gpt4 book ai didi

python - 运行测试前的 Pytest 和数据库清理

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:26 25 4
gpt4 key购买 nike

我正在使用 Flask 构建 Web 服务并使用 pytest 进行测试

我正在使用 pytest fixtures 来设置和拆除测试资源,但我需要测试一个将在数据库中创建一些记录的 POST 端点

我们如何清理这些记录?

最佳答案

您可以 use a fixture to do that cleanup .

@pytest.fixture
def cleanup():
yield
# This is executed when the test using the fixture is done
db_cleanup()

def test_records_created(cleanup): # pylint: disable=redefined-outer-name,unused-argument
response = app.test_client().post('/path', json=payload)
assert response.status_code == 200
assert ...

关于python - 运行测试前的 Pytest 和数据库清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com