requests与Unittest的结合形成自动化测试
py文件1
import json
import unittest,requests,parameterized
def yun_login(username,password):
url = "http://yun.zenm.vip/iot/account/login/"
header = {
"Content-Type":"application/json"
}
print(username,password)
data = {
‘username‘:username,
‘password‘:password
}
json_data = json.dumps(data)
res = requests.post(url=url,data=json_data,headers=header)
json_data = res.json()
print(json_data)
return json_data
class Yun_loginTest(unittest.TestCase):
# 正确的用户名密码
@parameterized.parameterized.expand([(‘peter‘,‘admin‘,200),(‘peter1‘,‘admin‘,4),(‘peter‘,‘admin1‘,5)])
def test01(self,username,password,code):
username=‘peter‘
password=‘admin‘
code = 200
json_data = yun_login(username,password)
self.assertEqual(int(json_data[‘result‘]),code)
py文件2
import unittest
from yun.yun_test import Yun_loginTest
from HTMLTestRunner import HTMLTestRunner
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Yun_loginTest))
f = open("yun_login_test.html", ‘wb‘)
runner = HTMLTestRunner(stream=f,title=‘yun登录测试报告‘,description=‘Edge‘)
runner.run(suite)