Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django test urls status code

Tags:

hello i create some test for my project mainly to enrich my knowledge.

and I have some questions.

simple test code :

test.py

from django.test import Client, TestCase
class User_Form_Test(TestCase):
    def test_logged_user_get_details(self):
        response = self.client.get('/details/', follow=True)
        self.assertEqual(response.status_code, 200)

    def test_logged_user_get_details_images(self):
        response = self.client.get('/details-images/', follow=True)
        self.assertEqual(response.status_code, 200)

urls.py

url(r'^details/(?P<id>\d+)/$', views.details, name='details'),
url(r'^details-images/(?P<slug>[^\.]+)/$', views.details_images, name='details_images')

all this code work fine I take passed messages in this two test.

my question is how to can test like this example all possible regex from id in first case and second slug in second case automate ?

like image 423
Mar Avatar asked Jan 03 '18 00:01

Mar


People also ask

How do I run a test in Django?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

What is assert in Django?

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.

What is client in Django test?

The test client. The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically.


1 Answers

First of all use path() and re_path(), I think url() is depreciated or even removed.
Looking at your regex pattern looks like you have made id and slug optional, which doesn't makes much sense, but its ok.
Now you want to check for all possible regex for id and slug but the total possible matches are infinite as any number will match id and anything will match slug, so its not possible.
I think for your first case in your views you will look for an object with that id in your database and then show details about it on the page you render. Therefore in your setUp() method you should create a few objects of your models, eg.:

def setUp(self):
    MyModel.objects.create(id=1)
    MyModel.objects.create(id=5)
    ...

Now in your test check for these id:

def test_logged_user_get_details(self):
    response = self.client.get(reverse('details', args=(1,)), follow=True)    # for first object
    self.assertEqual(response.status_code, 200)
    response = self.client.get(reverse('details', args=(5,)), follow=True)    # for second object
    self.assertEqual(response.status_code, 200)

Also check for a id that doesn't exists:

response = self.client.get(reverse('details', args=(2,)), follow=True)    # for object that doesn't exists
self.assertEqual(response.status_code, 404)

Since name of test suggests only logged in user can access the given webpage so, your setup should contain this (assuming you are using django's auth):

self.user = User.objects.create_user('test', '[email protected]', 'testtest')

Then in every test that requires login, first do this:

self.client.force_login(self.user)

As you said in your question you want to check for all possible regex matches which is not possible, but for some reason if you want to check for many id or slug you can use a loop:

for i in range(1,n):    # define n
    response = self.client.get(reverse('details', args=(i,)), follow=True)
    self.assertEqual(response.status_code, 200)

to me doing something like this makes no sense but since since I don't know completely about how your app works, it might be of some use to you.

like image 121
Vaibhav Vishal Avatar answered Sep 20 '22 12:09

Vaibhav Vishal