gpt4 book ai didi

python - Django:如何测试 'HttpResponsePermanentRedirect'

转载 作者:太空狗 更新时间:2023-10-29 22:00:21 26 4
gpt4 key购买 nike

我正在为我的 Django 应用编写一些测试。在我看来,它使用“HttpResponseRedirect”重定向到其他一些 URL。那么我该如何测试呢?

最佳答案

Django TestCase 类有一个方法 assertRedirects你可以使用。

from django.test import TestCase

class MyTestCase(TestCase):

def test_my_redirect(self):
"""Tests that /my-url/ permanently redirects to /next-url/"""
response = self.client.get('/my-url/')
self.assertRedirects(response, '/next-url/', status_code=301)

状态代码 301 检查它是否是永久重定向。

关于python - Django:如何测试 'HttpResponsePermanentRedirect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11683347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com