我正在使用 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/