Web項目如何做單元測試

  • 2019 年 10 月 3 日
  • 筆記

你可能會用單元測試框架,python的unittest、pytest,Java的Junit、testNG等。

那麼你會做單元測試么!當然了,這有什麼難的?

test_demo.py

def inc(x):      return x + 1      def test_answer():      assert inc(3) == 4

inc() 是定義的一個被測函數,test_anserver() 用於測試上面的一段程式碼。

通過pytest運行上面的程式碼:

> pytest test_demo.py  ====================== test session starts ======================= platform win32 -- Python 3.7.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0  rootdir: D:vipcndemo  plugins: cov-2.7.1, forked-1.0.2, html-1.20.0, metadata-1.8.0, ordering-0.6, parallel-0.0.9, rerunfailures-7.0, xdist-1.28.0, seleniumbase-1.23.10  collected 1 item    test_demo.py .                                              [100%]    ==================== 1 passed in 0.08 seconds ====================

單元測試不就是這麼單嘛!


那麼Web項目中的單元測試如何做?

我們以Django Web框架為例,它是MTV開發模式。接下來會圍繞著這個模式介紹如何做測試。

模型測試

  • M 指models,用於定義ORM,即對象關係映射,是通過使用描述對象和資料庫之間映射的元數據,將面向對象語言程式中的對象自動持久化到關係資料庫中。

models.py 中的程式碼是這樣的:

from django.db import models      class Question(models.Model):      question_text = models.CharField(max_length=200)      pub_date = models.DateTimeField(auto_now=True)  

這裡定義了兩個類,這兩個類即沒有入參,也沒有return返回值。如何測試呢?

測試程式碼如下:

from django.test import TestCase  from myapp.models import Question    class QuestionTestCase(TestCase):        def setUp(self):          Question.objects.create(id=1, question_text="你會做單元測試么?")        def test_question(self):          """查詢id=1的問題"""          question = Question.objects.get(id=1)          self.assertEqual(question.question_text, '你會做單元測試么?')  

不知道你是否看懂了這段程式碼,django模型我們可以看作是資料庫表,那麼對於表的操作就是增刪改查,這裡先創建一條數據,再查詢出這條數據,然後判斷其欄位是否正確。

參考:https://docs.djangoproject.com/en/2.2/topics/testing/overview/

視圖測試

  • V 指views,用於接收前端發來的請求,可能需要調用資料庫,把對應的數據處理之後,和HTML頁面一同返回給前端。

views.py 程式碼如下:

from django.shortcuts import render  from .models import Question      def index(request):      latest_question_list = Question.objects.order_by('-pub_date')[:5]      context = {'latest_question_list': latest_question_list}      return render(request, 'polls/index.html', context)

index() 視圖函數確實有入參,request包含的是客戶端資訊,比如請求的方法,請求的host,請求頭Header等,這些客戶端數據如何構造? return返回的是HTML頁面,以及查詢資料庫的數據,如何針對這些數據寫斷言呢?

測試程式碼如下:

from django.test import TestCase  from myapp.models import Question    class IndexTestCase(TestCase):        def setUp(self):          Question.objects.create(id=1, question_text="你會做單元測試么?")        def test_index(self):          """測試index視圖"""          response = self.client.get("/index")          self.assertEqual(response.status_code, 200)          self.assertTemplateUsed(response, "polls/index.html")

這裡假定當瀏覽器訪問 http://127.0.0.1:8000/index 時調用到index視圖,返問題列表頁面。

self.client.get() 可以模擬客戶端瀏覽器發送 request GET 請求。拿到服務端的response,判斷狀態碼是否為 200。 self.assertTemplateUsed() 斷言返回的頁面是否正確。

參考:https://docs.djangoproject.com/en/2.2/topics/testing/tools/

模板測試

  • T 指Teamplate,主要是HTML頁面。用戶在瀏覽器中輸入URL地址,最終會得到一個HTML頁面。

index.html程式碼如下:

{% if latest_question_list %}      <ul>      {% for question in latest_question_list %}          <li><a name="q" href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>      {% endfor %}      </ul>  {% else %}      <p>No polls are available.</p>  {% endif %}

這裡面的程式碼連個方法都沒有,更別提入參和返回值了,請問怎麼對HTML程式碼進行測試?

我們確實沒有辦法直接對HTML程式碼進行測試。不過,可以藉助Selenium來做UI自動化測試,從而保證頁面的正確性。

from django.contrib.staticfiles.testing import StaticLiveServerTestCase  from selenium import webdriver    class MySeleniumTests(StaticLiveServerTestCase):        @classmethod      def setUpClass(cls):          super().setUpClass()          cls.selenium = webdriver.Chrome()          cls.selenium.implicitly_wait(10)        @classmethod      def tearDownClass(cls):          cls.selenium.quit()          super().tearDownClass()        def test_index_page(self):          self.selenium.get('%s%s' % (self.live_server_url, '/index'))          question_list = self.selenium.find_elements_by_name("q")          for q in question_list:              print(q.text)

Django封裝了StaticLiveServerTestCase,讓你在運行UI測試時會自動啟動Django服務。 所以,你可以直接使用self.live_server_url 訪問django啟動的服務地址。

參考:https://docs.djangoproject.com/en/2.2/topics/testing/tools/

本文是否刷新了你對項目中單元測試的理解?那麼問題來了,我上面的寫的哪部分程式碼屬於 單元測試