[PYTHON] Debugging and testing reports

Let's try debugging and testing using Python with the blog app created in the TODO task of Part3.

The URL of the app code is https://github.com/maeclamar/todo-part3 is.

Debugging with Pdb

Let's debug using Pdb. For how to use Pdb, I referred to this article. Open views.py and add the Pdb import statement to your code.

blogs/views.py


def detail(request, blog_id):
    import pdb; pdb.set_trace() #add to
    blog = Blog.objects.get(id=blog_id)
    comments = Comment.objects.filter(post=blog)

    if request.method == "POST":
        form = forms.CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = blog
            comment.author = request.user
            comment.save()
#            form = forms.CommentForm() #Comment out once

In this state, fill in the comment form in one of the articles and send it. The comment will then be reflected in the comment field, but the form will retain what you previously entered. Debug to detect the cause of this issue.

Start the server.

terminal


$ python manage.py runserver

Once launched, fill out the comment form in one of the blog posts and submit. Then

terminal


> /Users/Kazuki/todo/django_blog/blogs/views.py(16)detail()
-> blog = Blog.objects.get(id=blog_id)

Is displayed. If you enter `` `l``` here,

terminal


> /Users/Kazuki/todo/django_blog/blogs/views.py(16)detail()
-> blog = Blog.objects.get(id=blog_id)
(Pdb) l
 14     def detail(request, blog_id):
 15         import pdb; pdb.set_trace()
 16  ->     blog = Blog.objects.get(id=blog_id)
 17         comments = Comment.objects.filter(post=blog)
 18
 19         if request.method == "POST":
 20             form = forms.CommentForm(request.POST)
 21             if form.is_valid():

And part of the code is displayed, with an arrow on the line to be executed. This time, I want to see what the form looks like at the time of `` `return``` on line 29, so I will set a breakpoint on line 29.

terminal


> /Users/Kazuki/todo/django_blog/blogs/views.py(16)detail()
-> blog = Blog.objects.get(id=blog_id)
(Pdb) b 29
Breakpoint 1 at /Users/kazuki/todo/django_blog/blogs/views.py:29

Proceed to the 29th line at once.

terminal


(Pdb) c
> /Users/Kazuki/todo/django_blog/blogs/views.py(29)
-> return render(request, 'blogs/detail.html', {'blog': blog, 'form':form, 'comments':comments})

Now let's check the state of the form.

terminal


(Pdb) p form
<CommentForm bound=True, valid=Unknown, fields=(text)>

The `bound``` of the form is now True```. This means that the form contains data that can be submitted. To put this in the `` False``` state, in the views.py file,

blogs/views.py


#            form = forms.CommentForm() #Comment out once

When I comment out a sentence and submit the form again, the form doesn't have any previously entered content. In fact, when I debug it,

terminal


(Pdb) p form
<CommentForm bound=False, valid=Unknown, fields=(text)>

Is displayed, and `bound` is `` `False```.

test

Next, I will write a test for the app.

blogs/test.py


from django.urls import resolve
from django.test import TestCase
from blogs.views import index, detail, signup

from django.http import HttpRequest
from django.template.loader import render_to_string

#Verify that the corresponding view is launched at each URL
class UrlResolveTest(TestCase):
    def test_url_resolves_to_index_view(self):
        found = resolve('/')
        self.assertEqual(found.func, index)

    def test_url_resolves_to_detail_view(self):
        found = resolve('/detail/3/')
        self.assertEqual(found.func, detail)

    def test_url_resolves_to_signup_view(self):
        found = resolve('/signup/')

#Verify that the correct HTML is returned
class HtmlTest(TestCase):
    def test_index_page_returns_correct_html(self):
        request = HttpRequest()
        response = index(request)
        expected_html = render_to_string('blogs/index.html', {'blogs': []})
        self.assertEqual(response.content.decode(), expected_html)

I will test it.

terminal


$ python manage.py test
Creating test database for alias 'default'...
...
----------------------------------------------------------------------
Ran 4 tests in 0.060s

OK
Destroying test database for alias 'default'...

I passed the test.

Recommended Posts

Debugging and testing reports
Error classification (python3.x) and Debugging notes
Tips for replacing and debugging functions
6 Python libraries for faster development and debugging
Loading and testing Chainer's trained imagenet model