如何使用 Pytest 在 Django 中测试缓存存储(Redis)?

Vic*_*tak 5 django caching redis pytest pytest-django

我正在使用 Django 1.11.9 和django-pytest库来测试我的应用程序。另外,我使用 Redis 作为缓存存储。我的问题是 \xe2\x80\x94 如何制作测试缓存存储并在运行测试之前使用测试数据设置它?与 Django 数据库的方式类似。

\n\n

我想添加一些key: value数据来测试缓存存储(在 Redis 中),运行测试,然后删除所有这些测试数据(清除测试缓存)。

\n

Vic*_*tak 1

我在py.test文档中找到了解决方案:

同样,在每个方法调用周围都会调用以下方法:

def setup_method(self, method):
    """ setup any state tied to the execution of the given method in a
    class.  setup_method is invoked for every test method of a class.
    """

def teardown_method(self, method):
    """ teardown any state that was previously setup with a setup_method
    call.
    """
Run Code Online (Sandbox Code Playgroud)

请参阅: https: //docs.pytest.org/en/latest/xunit_setup.html ?highlight=setup_class#method-and-function-level-setup-teardown

  • 我有同样的问题,但我不知道这些方法有什么帮助。您能再描述一下解决方案吗?最好有例子? (3认同)