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

标签 python python-3.x testing integration-testing pytest

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

相关文章:

python - 在 Mac 上使用终端安装 NLTK for Python 3.3

Python3 : Wait for Daemon to finish iteration

xml - Jmeter- 如何从 HTTP 采样器的响应中读取 xml 属性值

ruby - 内存数据库中的数据映射器

android - 如何将 R.ids 链接到实际的按钮 android 测试

python - Spark 上下文初始化后,在运行时期间更改 pyspark 的 hadoop 配置中的 aws 凭证

Python逆幂集生成器

python - 在 python 中绘制像素的最佳方法

python - 递归总是更新字典

python - 如何在二维数组中输入元素