How can I do such a typical unittest?

This is a simple structure in my project:

MyAPP---
        note---
               __init__.py
               views.py
               urls.py
               test.py
               models.py
        auth--
              ...
        template---
                   auth---
                          login.html
                          register.html
                   note---
                          noteshow.html
                   media---
                           css---
                                 ...
                           js---
                                 ...
        settings.py
        urls.py
        __init__.py
        manage.py

I want to make unittest, which can check the notes page working propeyly or not.

The code:

from django.test import TestCase

class Note(TestCase):
    def test_noteshow(self):
        response = self.client.get('/note/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, '/note/noteshow.html')

The problem is that my project includes auth mod, it will force the unlogin user to redirect to the login.html page when they visit noteshow.html.

So, when I run my unittest, in bash it crashes that response.status_code is always 302 instead of 200.

Well, although through this result I can verify that the auth mod works well, this is not what I want.

OK, the question is how can I do another unittest to check my notes. Used or not?

Thanks to everyone.

django version: 1.1.1

python version: 2.6.4

Use Eclipse for MAC OS

+3
1

. - setUp(), , , . tearDown(), ( ).

setUp() tearDown() .

:

class Note(TestCase):
    def setUp(self):
        self.client = Client()
        self.new_user = User.objects.create_user('blah', 'blah@blah.com', 'blah')
        self.new_user.save()
        self.client.login(username='blah', password='blah')

    def tearDown(self):
        self.client.logout()
        self.new_user.delete()

    def test_noteshow(self):
        response = self.client.get('/note/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, '/note/noteshow.html')

, .

+6

Source: https://habr.com/ru/post/1744724/


All Articles