pip install nose pip install nose-html-reporting pip install requests
#coding=utf-8 import requests import json url = 'https://api.douban.com/v2/movie/search' params=dict(q=u'劉德華') r = requests.get(url, params=params) print 'Search Params:\n', json.dumps(params, ensure_ascii=False) print 'Search Response:\n', json.dumps(r.json(), ensure_ascii=False, indent=4)
class test_doubanSearch(object): @staticmethod def search(params, expectNum=None): url = 'https://api.douban.com/v2/movie/search' r = requests.get(url, params=params) print 'Search Params:\n', json.dumps(params, ensure_ascii=False) print 'Search Response:\n', json.dumps(r.json(), ensure_ascii=False, indent=4) def test_q(self): # 校驗搜索條件 q qs = [u'白夜追兇', u'大話西遊', u'周星馳', u'張藝謀', u'周星馳,吳孟達', u'張藝謀,鞏俐', u'周星馳,大話西遊', u'白夜追兇,潘粵明'] for q in qs: params = dict(q=q) f = partial(test_doubanSearch.search, params) f.description = json.dumps(params, ensure_ascii=False).encode('utf-8') yield (f,)
class check_response(): @staticmethod def check_result(response, params, expectNum=None): # 因爲搜索結果存在模糊匹配的狀況,這裏簡單處理只校驗第一個返回結果的正確性 if expectNum is not None: # 指望結果數目不爲None時,只判斷返回結果數目 eq_(expectNum, len(response['subjects']), '{0}!={1}'.format(expectNum, len(response['subjects']))) else: if not response['subjects']: # 結果爲空,直接返回失敗 assert False else: # 結果不爲空,校驗第一個結果 subject = response['subjects'][0] # 先校驗搜索條件tag if params.get('tag'): for word in params['tag'].split(','): genres = subject['genres'] ok_(word in genres, 'Check {0} failed!'.format(word.encode('utf-8'))) # 再校驗搜索條件q elif params.get('q'): # 依次判斷片名,導演或演員中是否含有搜索詞,任意一個含有則返回成功 for word in params['q'].split(','): title = [subject['title']] casts = [i['name'] for i in subject['casts']] directors = [i['name'] for i in subject['directors']] total = title + casts + directors ok_(any(word.lower() in i.lower() for i in total), 'Check {0} failed!'.format(word.encode('utf-8'))) @staticmethod def check_pageSize(response): # 判斷分頁結果數目是否正確 count = response.get('count') start = response.get('start') total = response.get('total') diff = total - start if diff >= count: expectPageSize = count elif count > diff > 0: expectPageSize = diff else: expectPageSize = 0 eq_(expectPageSize, len(response['subjects']), '{0}!={1}'.format(expectPageSize, len(response['subjects'])))
nosetests -v test_doubanSearch.py:test_doubanSearch --with-html --html-report=TestReport.html
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def send_mail(): # 讀取測試報告內容 with open(report_file, 'r') as f: content = f.read().decode('utf-8') msg = MIMEMultipart('mixed') # 添加郵件內容 msg_html = MIMEText(content, 'html', 'utf-8') msg.attach(msg_html) # 添加附件 msg_attachment = MIMEText(content, 'html', 'utf-8') msg_attachment["Content-Disposition"] = 'attachment; filename="{0}"'.format(report_file) msg.attach(msg_attachment) msg['Subject'] = mail_subjet msg['From'] = mail_user msg['To'] = ';'.join(mail_to) try: # 鏈接郵件服務器 s = smtplib.SMTP(mail_host, 25) # 登錄 s.login(mail_user, mail_pwd) # 發送郵件 s.sendmail(mail_user, mail_to, msg.as_string()) # 退出 s.quit() except Exception as e: print "Exceptioin ", e
打開nosetests運行完成後生成的測試報告,能夠看出本次測試共執行了51條測試用例,50條成功,1條失敗。python
失敗的用例能夠看到傳入的參數是:{"count": -10, "tag": "喜劇"},此時返回的結果數與咱們的指望結果不一致(count爲負數時,指望結果是接口報錯或使用默認值20,但實際返回的結果數目是189。趕忙去給豆瓣提bug啦- -)git
python test_doubanSearch.py