我正在使用Django Web应用程序,它需要查询PostgreSQL数据库.使用Python 线程接口实现并发时,我收到DoesNotExist查询项的错误.当然,顺序执行查询时不会发生这些错误.
让我展示一个单元测试,我写这个测试来演示意外的行为:
class ThreadingTest(TestCase):
fixtures = ['demo_city',]
def test_sequential_requests(self):
"""
A very simple request to database, made sequentially.
A fixture for the cities has been loaded above. It is supposed to be
six cities in the testing database now. We will made a request for
each one of the cities sequentially.
"""
for number in range(1, 7):
c = City.objects.get(pk=number)
self.assertEqual(c.pk, number)
def test_threaded_requests(self):
"""
Now, to test the threaded behavior, we will spawn a …Run Code Online (Sandbox Code Playgroud) 我正在测试一组Django应用程序,它们广泛使用'permission_required'装饰器.这在我拥有的大多数视图中都得到了302 HTTP响应.
我的问题是:有什么方法可以避免或停用测试中的'permission_required',所以当我调用我的视图而不是302时,我可以获得200响应?
谢谢!