如何使用django-pytest跟随Django重定向? [英] How to follow Django redirect using django-pytest?

查看:166
本文介绍了如何使用django-pytest跟随Django重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置

In setting up a ArchiveIndexView in Django I am able to successfully display a list of items in a model by navigating to the page myself.

当要在pytest中编写测试以验证导航到"checklist_GTD/archive/"页面是否成功时,测试失败并显示以下消息:

When going to write the test in pytest to verify navigating to the page "checklist_GTD/archive/" succeeds, the test fails with the message:

>       assert response.status_code == 200
E       assert 301 == 200
E        +  where 301 = <HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/checklist_GTD/archive/">.status_code

test_archive.py:4: AssertionError

我知道有一种方法可以遵循请求以获取最终的status_code.有人可以帮我在pytest-django中完成此工作,类似于文档上没有任何重定向.谢谢.

I understand there is a way to follow the request to get the final status_code. Can someone help me with how this done in pytest-django, similar to this question? The documentation on pytest-django does not have anything on redirects. Thanks.

推荐答案

更新: 我被淘汰了,但我仍然认为我的回答更好,所以让我解释一下.

UPDATE: I'm getting downvoted into oblivion but I still think my answer is better so let me explain.

我仍然认为Shacker的答案存在问题,您可以设置follow = True并获得200的响应代码,但不能使用您期望的URL.例如,您可能会意外地重定向到登录页面,然后遵循并获得200的响应代码.

I still think there is a problem with Shacker's answer, where you can set follow=True and get a response code of 200 but not at the URL you expect. For example, you could get redirected unexpectedly to the login page, follow and get a response code of 200.

我了解我问了一个关于如何使用pytest完成某事的问题,而我被拒绝的原因是因为我使用Django的内置TestCase类提供了答案.但是,对于我而言,测试的正确答案对我而言比当时专门使用pytest更重要.如下所述,我的答案仍适用于pytest的测试发现,因此我认为答案仍然有效.毕竟,pytest是基于Django的内置TestCase构建的.我的回答断言200的响应代码来自我期望的来源.

I understand that I asked a question on how to accomplish something with pytest and the reason I'm getting downvoted is because I provided an answer using Django's built-in TestCase class. However, the correct answer for the test is/was more important to me at the time than exclusively using pytest. As noted below, my answer still works with pytest's test discovery so I think the answer is still valid. After all, pytest is built upon Django's built-in TestCase. And my answer asserts the response code of 200 came from where I expected it to come from.

最好的解决方案是修改pytest以将expected_url作为参数.如果有人愿意这样做,我认为这将是一个很大的改进.感谢您的阅读.

The best solution would be to modify pytest to include the expected_url as a parameter. If anyone is up for doing this I think it would be a big improvement. Thanks for reading.

原始内容:

在这里回答我自己的问题.我决定使用内置的Django测试框架的assertRedirects包含最终的预期URL,并验证它(1)最初使用302响应进行了重定向,并且(2)最终在代码处通过200 成功了预期的网址.

Answering my own question here. I decided to include final expected URL using the built-in Django testing framework's assertRedirects and verify that it (1) gets redirected initially with 302 response and (2) eventually succeeds with a code 200 at the expected URL.

from django.test import TestCase, Client

def test_pytest_works():
    assert 1==1

class Test(TestCase):
    def test_redirect(self):
        client = Client()
        response = client.get("/checklist_GTD/archive/")
        self.assertRedirects(response, "/expected_redirect/url", 302, 200)

@tdsymonds的提示给我指出正确的方向.我很感谢Shacker的回答,但是在某些情况下,当页面被重定向到不良URL时,重定向结果为200.使用上述解决方案,我可以强制执行重定向URL, pytest-django可以执行的操作目前不支持.

Hat tip to @tdsymonds for pointing me in the right direction. I appreciated Shacker's answer but I have seen in some scenarios the redirect result being 200 when the page is redirected to an undesirable URL. With the solution above I am able to enforce the redirect URL, which pytest-django does not currently support.

请注意:此答案与pytest-django的自动发现功能兼容,因此不兼容(它将自动发现pytest-django和Django TestCase测试).

Please note: This answer is compliant with the auto-discover feature of pytest-django and is thus not incompatible (it will auto-discover both pytest-django and Django TestCase tests).

这篇关于如何使用django-pytest跟随Django重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆