Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test redirection in Django using pytest?

I already know that one can implement a class that inherits from SimpleTestCase, and one can test redirection by:

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

However, I am wondering what is the way I can check for redirection using 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'))

However, I am getting the following error:

  SimpleTestCase.assertRedirects(response, reverse('pledges:home'))

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

Is there any way I can use pytest to verify redirection with Django? Or I should go the way using a class that inherits from SimpleTestCase?

like image 805
lmiguelvargasf Avatar asked Jan 17 '18 04:01

lmiguelvargasf


People also ask

Does Django test use Pytest?

pytest-django Documentation. pytest-django is a plugin for pytest that provides a set of useful tools for testing Django applications and projects.

How pass data redirect Django?

shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.

Does Django use Pytest or Unittest?

Django's unit tests use a Python standard library module: unittest . This module defines tests using a class-based approach.

What is Pytest Mark Django_db?

pytest.mark. django_db([transaction=False, reset_sequences=False, databases=None]) This is used to mark a test function as requiring the database. It will ensure the database is set up correctly for the test. Each test will run in its own transaction which will be rolled back at the end of the test.


2 Answers

This is an instance method, so it will never work like a class method. You should be able to simply change the line:

SimpleTestCase.assertRedirects(...)

into:

SimpleTestCase().assertRedirects(...)

i.e. we're creating an instance in order to provide a bound method.

like image 103
wim Avatar answered Oct 07 '22 10:10

wim


I'm not an expert with pytest so probably is not elegant but you can check for the Location header like

test_whatever(self, user):
    client = Client()
    url = reverse('admin:documents_document_add')
    client.force_login(user)

    response = client.post(url, {<something>})

    # if the document is added correctly we redirect
    assert response.status_code == 302
    assert response['Location'] == reverse('admin:documents_document_changelist')
like image 2
gipi Avatar answered Oct 07 '22 09:10

gipi