1.代码如下所示:
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.生成的测试报告如下所示: