我有一个涉及多个Django视图的测试
在我尝试运行以下代码的多个视图之间似乎没有共享伪造代码:
import fakeredis
from testfixtures import Replacer
class TestWithFakeRedis(TestCase):
def setup_redis(self, test_func):
fake_redis = fakeredis.FakeStrictRedis()
with Replacer() as replace:
replace('app1.views.redis_connection', fake_redis)
replace("app2.views.redis_connection", fake_redis)
replace("app2.views.redis_connection", fake_redis)
test_func(fake_redis)
def test_something(self):
def test_func(redis_connection):
# some testing coded here
pass
self.setup_redis(test_func)假象不能在多个视图之间传递,这是我需要的东西
提前谢谢你,
纳达夫
发布于 2020-06-10 06:54:28
我的解决方案涉及使用unittest.mock.patch:
import fakeredis
fake_redis = fakeredis.FakeRedis()
@patch("app_name1.views.redis_connection", fake_redis)
@patch("app_name2.views.redis_connection", fake_redis)
@patch("app_name3.views.redis_connection", fake_redis)
class TestSomethingWithRedis(TestCase):
pass如果要检查测试中的查询,请使用雷迪斯
https://stackoverflow.com/questions/62292568
复制相似问题