其實自動化測試的程式我覺得比其他東西都難寫很多
我們系上很多人畢業前出去做了QA的實習(包括我)
但是所謂的QA卻只是無聊的做著人工檢測
直到現在我仍然不是很明白自動化測試到底該怎麼實作到實際的大型專案中
尤其是當系統很大時,牽一髮而動全身。

自動化測試

  1. 使用Shell發現BUG(未來時間發布的問題應該回傳FALSE,結果仍需由人工判斷)

  2. 使用自動測試->在test.py裡將在Shell中進行的測試寫成class

  3. 接著在cmd中執行test,Django會替我們建置資料庫去測試回傳值是否正確
    可以看到回傳的是AssertionError(判斷錯誤)

  4. 既然已經發現BUG,我們就去修改models.py中的was_published_recently()函數

  5. 接著再重新測試一次

  6. 為測試再加上兩個測試函數(old & recent)

  7. 也為IndexView和DetailView建立測試函數

  8. test.py加上測試

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
        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.
    """
    create_question(question_text="Past question.", days=-30)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
    response.context['latest_question_list'],
    ['<Question: Past 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.
    """
    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: Past question.>']
    )

    def test_two_past_questions(self):
    """
    The questions index page may display multiple questions.
    """
    create_question(question_text="Past question 1.", days=-30)
    create_question(question_text="Past question 2.", days=-5)
    response = self.client.get(reverse('polls:index'))
    self.assertQuerysetEqual(
    response.context['latest_question_list'],
    ['<Question: Past question 2.>', '<Question: Past question 1.>']
    )
    class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
    """
    The detail view of a question with a pub_date in the future
    returns a 404 not found.
    """
    future_question = create_question(question_text='Future question.', days=5)
    url = reverse('polls:detail', args=(future_question.id,))
    response = self.client.get(url)
    self.assertEqual(response.status_code, 404)

    def test_past_question(self):
    """
    The detail view of a question with a pub_date in the past
    displays the question's text.
    """
    past_question = create_question(question_text='Past Question.', days=-5)
    url = reverse('polls:detail', args=(past_question.id,))
    response = self.client.get(url)
    self.assertContains(response, past_question.question_text)