Web項目如何作單元測試

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

那麼你會作單元測試麼!固然了,這有什麼難的?前端

test_demo.pypython

def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 4

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

經過pytest運行上面的代碼:shell

> 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:\vipcn\demo
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

咱們以Django Web框架爲例,它是MTV開發模式。接下來會圍繞着這個模式介紹如何作測試。瀏覽器

模型測試

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

models.py 中的代碼是這樣的:session

from django.db import models


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

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

測試代碼以下:

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/

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

相關文章
相關標籤/搜索