0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Django使い始めの備忘録(テスト)

Posted at

概要

色々あってPython/Djangoを使ってWebアプリを作ることになりそうなので、
Djangoでできるテスト用鵜環境構築等の自分用備忘録。
Webアプリ実装についてはDjango公式ドキュメント拙作備忘録を確認してください。

環境

OS : Windows 11
Python : 3.10.7
Django : 4.1.1

注意事項

コマンド実行にPowerShellを使っていて、管理者権限で実行しないと動作しない場合があります。

自動テスト作成

テスト対象は以下の記事を参考に作成した投票のサンプルアプリです。

その中のmodels.pyをテスト対象にします。

models.py
from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text
    def is_voted(self):
        return self.votes > 0

自動テストコードはDjango公式ドキュメントを参考にして作成します。

tests.py
from django.test import TestCase

from .models import Choice

class ChoiceModelTests(TestCase):

    def test_is_voted_ok(self):
        voted_choice = Choice(votes=1)
        self.assertIs(voted_choice.is_voted(), True)
    def test_is_voted_ng(self):
        new_choice = Choice()
        self.assertIs(new_choice.is_voted(), False)

作成したテストコードを以下のコマンドで実行します。
py manage.py test polls

実行結果は以下の通りです。
image.png

次にviews.pyのclass IndexView(generic.ListView)をテスト対象にして、テストコードを作成します。
※投票のサンプルアプリに登録したデータを削除しておきます。

views.py
import datetime

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils import timezone
from django.views import generic

from .models import Choice, Question

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        return Question.objects.filter(
            pub_date__lte=timezone.now()
        ).order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

    def vote(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        try:
            selected_choice = question.choice_set.get(pk=request.POST['choice'])
        except (KeyError, Choice.DoesNotExist):
            # Redisplay the question voting form.
            return render(request, 'polls/detail.html', {
                'question': question,
                'error_message': "You didn't select a choice.",
            })
        else:
            selected_choice.votes += 1
            selected_choice.save()
            # Always return an HttpResponseRedirect after successfully dealing
            # with POST data. This prevents data from being posted twice if a
            # user hits the Back button.
            return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
tests.py
import datetime

from django.urls import reverse
from django.test import TestCase
from django.utils import timezone

from .models import Choice, Question

def create_question(question_text, days):
    """
    Create a question with the given `question_text` and published the
    given number of `days` offset to now (negative for questions published
    in the past, positive for questions that have yet to be published).
    Do not insert this method into class QuestionIndexViewTests(TestCase).
    """
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)

class QuestionIndexViewTests(TestCase):

    def test_no_questions(self):
        """
        If no questions exist, an appropriate message is displayed.
        """
        response = self.client.get(reverse('polls:index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

    def test_past_question(self):
        """
        Questions with a pub_date in the past are displayed on the
        index page.
        """
        question = create_question(question_text="Past question.", days=-30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            [question],
        )

    def test_future_question(self):
        """
        Questions with a pub_date in the future aren't displayed on
        the index page.
        """
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

    def test_future_question_and_past_question(self):
        """
        Even if both past and future questions exist, only past questions
        are displayed.
        """
        question = create_question(question_text="Past question.", days=-30)
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            [question],
        )

    def test_two_past_questions(self):
        """
        The questions index page may display multiple questions.
        """
        question1 = create_question(question_text="Past question 1.", days=-30)
        question2 = create_question(question_text="Past question 2.", days=-5)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            [question2, question1],
        )

作成したテストコードを以下のコマンドで実行します。
py manage.py test polls

実行結果は以下の通りです。
image.png

この他にもSeleniumとの連携 LiveServerTestCaseについても公式ドキュメントで紹介されています。

最後に

アプリ作成の時と同様に、学習コストはどれくらいだろうと少し身構えていましたが、
チュートリアルの所要時間は半日足らずだったかと思います。
最後まで読んでいただき、ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?