微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用pytest在Django中测试重定向?

我已经知道可以实现一个继承自SimpleTestCase的类,并且可以通过以下方式测试重定向

SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix='', fetch_redirect_response=True)

但是,我想知道我可以使用pytest检查重定向的方式是什么:

@pytest.mark.django_db
def test_redirection_to_home_when_group_does_not_exist(create_social_user):
    """Some docstring defining what the test is checking."""
    c = Client()
    c.login(username='TEST_USERNAME', password='TEST_PASSWORD')
    response = c.get(reverse('pledges:home_group',
                             kwargs={'group_id': 100}),
                     follow=True)
    SimpleTestCase.assertRedirects(response, reverse('pledges:home'))

但是,我收到以下错误

06002

E TypeError: assertRedirects() missing 1 required positional argument: ‘expected_url’

有什么办法可以用pytest来验证Django的重定向吗?或者我应该使用继承自SimpleTestCase的类?

解决方法:

这是一个实例方法,因此它永远不会像类方法一样工作.你应该能够简单地改变这条线:

SimpleTestCase.assertRedirects(...)

成:

SimpleTestCase().assertRedirects(...)

即我们正在创建一个实例以提供绑定方法.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐