如何在 pytest-django 中模拟 django 设置属性

How to mock django settings attributes in pytest-django

使用 Django 默认单元测试时,修补设置属性很简单(例如使用 @override_settings 装饰器。)

我想覆盖测试方法设置的几个属性。当我使用 pytest-django 时,我怎样才能实现这个目标?

您可以将settings作为测试夹具传入,然后根据需要进行修改。这是来自 the documentation 的示例:

def test_with_specific_settings(settings):
    settings.USE_TZ = True
    assert settings.USE_TZ