1.代碼以下所示:html
import requests,unittest import json import HTMLTestRunner class TestDouBan(unittest.TestCase): def setUp(self): self.url = 'https://www.douban.com/accounts/login' self.data = { 'source':'index_nav', 'form_email':'帳號', 'form_password':'密碼' } def testLogin(self): r = requests.post(url=self.url,data=self.data) self.assertEqual(r.status_code,200) def testLoginError(self): r = requests.post(url=self.url,data=self.data) self.assertEqual(r.status_code,400) def testLoginResponseCode(self): r = requests.post(url=self.url,data=self.data) self.assertNotEqual(r,'<Response [200]>') def testLoginParams(self): r = requests.post(url=self.url,data=self.data) self.assertEqual(self.data.get('form_email'),'帳號') #斷言帳號密碼是否一致 self.assertEqual(self.data.get('form_password'), '密碼') self.assertEqual(self.data.get('source'), 'index_nav') if __name__ == '__main__': suit = unittest.TestSuite() #定義一個測試套件 suit.addTest(TestDouBan('testLogin')) #將測試用例添加到測試套件,根據實際需求添加多個測試用例 suit.addTest(TestDouBan('testLoginError')) suit.addTest(TestDouBan('testLoginResponseCode')) suit.addTest(TestDouBan('testLoginParams')) filename = 'E://htmlrunner.html' fp = open(filename,'wb') runner =HTMLTestRunner.HTMLTestRunner(stream=fp,title='豆瓣接口測試用例',description=u'接口列表') runner.run(suit) fp.close()
2.生成的測試報告以下所示:python