Django單元測試

前面簡單介紹了flask的單元測試,這裏說說Django單元測試。python

Django單元測試也是使用了python自帶的unittest,Django的testTestCase繼承了python的unittest.TestCasedjango

一、在建立Django app時,已經自動生成了tests.py文件,咱們直接在tests.py中編寫咱們的測試用例flask

from django.test import TestCase class GoodListView3Test(TestCase): def setUp(self): pass

    def test_goodlistview3(self): response = self.client.get('/goods/') self.assertEqual(response.status_code, 200)

  1)建立ModelTest類,繼承自django.test.TestCase測試類app

  2)定義setUp()方法,此方法,通常是用來作數據初始化單元測試

  3)經過 test_goodlistview3() 方法測試了經過url訪問 /goods/ 查詢數據,斷言返回的status_code是否爲200測試

 

二、執行tests.py文件url

  Django中,經過test命令能夠查找並運行全部TestCase的子類spa

  1)運行全部的測試用例code

    python manage.py testblog

  2)運行某個app下面的全部的測試用例

    python manage.py test someapp

  3)運行某個app下面的tests.py文件

    python manage.py test someapp.tests

  4)運行某個app下面的tests.py文件中指定的class類ModeTest

    python manage.py test someapp.tests.ModeTest

  5)執行ModeTest類下的某個測試方法(如上demo,執行GoodListView3Test下面的test_goodlistview3)

    python manage.py test someapp.tests.GoodListView3Test.test_goodlistview3

相關文章
相關標籤/搜索