不一樣的項目類型裏面的單元測試基本不一樣html
1.一、tests.py文件裏面寫入casepython
from django.test import TestCase from sign.models import Event,Guest #導入引用的模塊 # Create your tests here. class ModelsTest(TestCase): #建立測試類,繼承Django下面的TestCase方法 def test_event(self): #建立測試類 Event.objects.create(id=1, name="oneplus 3 event", status=True, limit=2000, address='shenzhen', start_time='2016-08-31 02:18:22') #數據初始化,建立一條發佈會數據來提供後面進行查找 results = Event.objects.all() #調用,並非真正的去查找相對應的數據,須要在前面先建立發佈會的字段才能夠查找到 print(results)
1.二、終端進入到相對應的目錄,而且使用Test方法運行腳本mysql
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py test Creating test database for alias 'default'... /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. result = self._query(query) /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release. result = self._query(query) System check identified no issues (0 silenced). <QuerySet [<Event: oneplus 3 event>]> . ---------------------------------------------------------------------- Ran 1 test in 0.005s OK Destroying test database for alias 'default'... /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. result = self._query(query) /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release. result = self._query(query)
1.三、運行原理sql
跟上面的unittest原理同樣shell
先找代碼中有沒有繼承Django中Test下面的TestCase類數據庫
再查找定義的方法中是否有test開頭的方法,若是有的話就執行django
case運行的前後順序爲:經過ASCII碼的排序進行排序運行ide
初始化的數據並不會到數據庫裏面,只是提供測試操做post
1.四、case的斷言操做單元測試
tests.py文件
from django.test import TestCase from sign.models import Event,Guest #導入引用的模塊 # Create your tests here. class ModelsTest(TestCase): #建立測試類,繼承Django下面的TestCase方法 def test_event(self): #建立測試類 Event.objects.create(id=1, name="test1", status=True, limit=2000, address='shenzhen', start_time='2016-08-31 02:18:22') #初始化測試數據,供下面查詢使用,這條數據並不會直接到數據庫裏面,只是提供測試查詢須要 results = Event.objects.get(name = 'test1') #查詢相對應的方式不會真正的去數據庫查詢數據,須要經過上面的方法建立數據 print(results.limit) print(results.address) self.assertEqual(results.limit,2000) #斷言查詢出來的數據是否limit = 2000 self.assertEqual(results.address,'shenzhen') #斷言查詢出來的數據 是否address = 'shenzhen'
終端運行結果
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py test Creating test database for alias 'default'... /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. result = self._query(query) /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release. result = self._query(query) System check identified no issues (0 silenced). 2000 shenzhen . ---------------------------------------------------------------------- Ran 1 test in 0.004s OK Destroying test database for alias 'default'... /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. result = self._query(query) /usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release. result = self._query(query)
1.五、使用setUp()、tearDown()
tests.py文件
from django.test import TestCase from sign.models import Event,Guest #導入引用的模塊 # Create your tests here. class ModelsTest(TestCase): #建立測試類,繼承Django下面的TestCase方法 def setUp(self): #初始化發佈會數據 Event.objects.create(id=1, name="test1", status=True, limit=2000, address='shenzhen', start_time='2016-08-31 02:18:22') #初始化嘉賓表數據 Guest.objects.create(id=2, event_id=1, realname='alen', phone='13711001101', email='alen@mail.com', sign=False) #發佈會的case def test_event(self): results =Event.objects.get(name = 'test1') print(results.limit) print(results.address) self.assertEqual(results.limit,2000) self.assertEqual(results.address,'shenzhen') #嘉賓表的case def test_guest(self): results =Guest.objects.get(realname= 'alen') self.assertEqual(results.phone,'13711001101') self.assertEqual(results.email,'alen@mail.com')
終端頁面
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py test
Creating test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
System check identified no issues (0 silenced).
2000
shenzhen
..
----------------------------------------------------------------------
Ran 2 tests in 0.010s
OK
Destroying test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
2.一、初始化測試環境
終端進入shell模式
導入Client類,模擬post、get請求,加入路徑,若是返回碼爲200則表示成功,創建了測試環境
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py shell Python 3.6.3 (default, Oct 17 2017, 00:27:06) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.test.utils import setup_test_environment >>> setup_test_environment() >>> >>> >>> >>> from django.test import Client #導入Client類 >>> c = Client() >>> response = c.get("/index/") #相似於模擬界面自動化測試的方法,加入相對應的跳轉後綴,模擬index頁面登陸效果 >>> response.status_code #獲取返回碼
200
2.二、測試case編寫
index頁面登陸case編寫
class TestIndex(TestCase): def test_idex(self): respose = self.client.get('/index/') print(respose.status_code)
運行結果
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py test
Creating test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
System check identified no issues (0 silenced).
200
.
----------------------------------------------------------------------
Ran 1 test in 0.017s
OK
Destroying test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
2.三、斷言判斷
class TestIndex(TestCase): def test_idex(self): respose = self.client.get('/index/') self.assertEqual(respose.status_code,200) #斷言狀態碼是否爲200 self.assertTemplateUsed(respose,'new_index.html') #斷言模板是否可用 # print(respose.status_code)
運行結果
monkey@MacBook-Pro-7 /usr/local/lib/python3.6/site-packages/guest python3 manage.py test
Creating test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.018s
OK
Destroying test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
result = self._query(query)
/usr/local/lib/python3.6/site-packages/pymysql/cursors.py:161: Warning: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
result = self._query(query)
2.四、登陸case編寫
from django.test import TestCase from sign.models import Event,Guest #導入引用的模塊 from django.contrib.auth.models import User #導入用戶表(在寫登陸單元測試case的時候須要建立一個用戶來測試 class LoginActionTest(TestCase): def setUp(self): ''' 初始化測試數據:建立一個新的用戶(與model層的初始化發佈會、嘉賓表數據同樣) :return: ''' User.objects.create_user('admin','admin@mail.com','admin123456') def test_add_author_email(self): ''' 添加測試用戶 :return: ''' user = User.objects.get(username='admin') self.assertEqual(user.username,'admin') self.assertEqual(user.email,'admin@mail.com') def test_login_action_username_password_null(self): ''' 用戶名、密碼爲空 :return: ''' response = self.client.post('/login_action/',{'input_username':'','input_password':''}) self.assertEqual(response.status_code,200) self.assertIn(b'username or password error!',response.content) #返回整個登陸頁面信息 def test_login_action_username_password_error(self): ''' 用戶名、密碼錯誤 :return: ''' test_data = {'username':'abc','password':'123'} response = self.c.post('/login_action/',data = test_data) self.assertEqual(response.status_code,200) self.assertIn(b'username or password error!',response.content) def test_login_action_success(self): ''' 登陸成功 :return: ''' test_data = {'username':'admin','password':'admin123456'} response = self.c.post('/login_actiom/',data = test_data) self.assertEqual(response.status_code,302)
只是一個HTML頁面,不能進行測試,若是HTML頁面有JS代碼(script)能夠進行測試
python3 manage.py test sign
python3 manage.py test sign.tests.TestIndex