您好,登錄后才能下訂單哦!
前段時間公司業務需要,準備把之前的接口自動化的腳本整理一下,把之前的冗長的代碼整理成一個個的用例,這樣代碼的可讀性就變得高了,而且相應的維護的成本降低。我們用到的是Pytest框架,關于框架的介紹及優點我這邊就不贅述了,這邊直接從實際的框架運用著手,相信大家對Pytest框架有個更加深刻的了解。
環境搭建
pip install pytest
安裝好了后直接
pytest version
實際運用
import pytest
import requests
import json
import time
@pytest.fixture()
def feeds_get():
feeds = requests.get(url='https://api.xxxx.xxxx.tv/feeds/feed/mix?devid=328ExxA-5954-4C29-9x48-AE0ExxB722B4&pageSize=9&uid=70192',
headers={'version': '32'})
JsonToDic = json.loads(feeds.text)
return JsonToDic
@pytest.fixture()
def feed_detail(feeds_get):
feedList = feeds_get['data']['list']
return feedList
@pytest.fixture()
def feeds_parsing_date(feed_detail):
crtTimeSortList = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
crtTimeSortList.append ( feedList['crtTimeSort'] )
return crtTimeSortList
@pytest.fixture()
def feeds_get_pidlist(feed_detail):
pidlist = []鄭州人流多少錢 http://www.hnmt120.com/
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
pidlist.append(feedList['pid'] )
return pidlist
@pytest.fixture()
def feeds_get_userid(feed_detail):
userid = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
userid.append(feedList['userid'])
return userid
@pytest.fixture()
def feeds_get_column(feed_detail):
columnlist = []
for feed_count in range(0,9):
feedList = (feed_detail[feed_count])
columnlist.append(feedList['column_id'] )
return columnlist
#服務器請求是否失敗
def test_feeds_connect(feeds_get):
assert feeds_get
#賬戶ID是否有推送
def test_DeviceId(feeds_get):
assert feeds_get['msg'] != 'recom result is not enough'
#檢查本次請求的feed數量是否為9
def test_feedcount(feed_detail):
assert len(feed_detail) == 9
#檢查重復內容
def test_repeatcontent(feeds_get_pidlist):
assert len(feeds_get_pidlist) == len(set(feeds_get_pidlist))
#檢查重復專欄
def test_repeatuser(feeds_get_column):
assert len(feeds_get_column) == len(set(feeds_get_column))
#檢查重復用戶
def test_repeatuser(feeds_get_userid):
assert len(feeds_get_userid) == len(set(feeds_get_userid))
#檢查文章視頻是否為三個月之內的
def test_feed_in90day(feeds_parsing_date):
now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
t = int(now_time)
pass_time = t - 300000000
for feed_count in range(0,9):
assert int(feeds_parsing_date[feed_count]) > pass_time
大家可以發現是不是每一條自動化的case都可以寫成一條assert語句,這樣自動化代碼的可讀性是不是提高了而且可維護性也提高了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。