Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

Testing urls django

I have a flatpage in my website. I created it by writing this:

urlpatterns = [path('admin/', admin.site.urls),path('facts/', include('django.contrib.flatpages.urls')),]

After that in admin page I created a page about-me, so the whole url for that page is ‘localhost/facts/about-me/’ I tried to write test for this page:

class StaticURLTests(TestCase):
    def setUp(self):
        self.guest_client = Client()

   def test_about_author(self):
       response = self.guest_client.get('/facts/about-me/')
       self.assertEqual(response.status_code, 200)

But I get the following error:

self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200
FAILED (failures=1)
Destroying test database for alias ‘default’…

Can’t figure out why. Maybe smb was facing the same problem.

I thought that the error was because of the flatpage but, the same error occured when I tried to test url of the Task model

urlpatterns = [path('task/<slug:slug>/',views.task, name='task'),]

Test:

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        Task = Task.objects.create(
            title='Title',
            slug=' test-slug',
            description='Desc',
        )


    def setUp(self):
        self.guest_client = Client()
        user = get_user_model()
        self.user = user.objects.create_user(username='Bob')

    def test_task_url_exists_at_desired_location_authorized(self):
        response = self.guest_client.get('/task/test-slug/')
        self.assertEqual(response.status_code, 200)

I tried to change response = self.guest_client.get('/task/test-slug/') to response = self.guest_client.get(reverse('/task/')) but nothing seems to work.

question from:https://stackoverflow.com/questions/65869148/testing-urls-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...