接口自动化
# 获取图片验证码import requestsresponse = requests.get(url="http://kdtx-test.itheima.net/api/captchaImage")print(response.status_code)
print(response.text)
import requestsurl = "http://kdtx-test.itheima.net/api/login"
header_data = {"Content-Type":"application/json"
}
login_data = {"username":"admin","password":"HM_2023_test","code":2,"uuid":"247dec0137944faeaf539d4d644ee92a"
}
response = requests.post(url=url,headers=header_data,json=login_data)print(response.status_code)
print(response.json())
# login api
import requestsclass LoginAPI:def __init__(self):self.url_verify = "http://kdtx-test.itheima.net/api/captchaImage"self.url_login = "http://kdtx-test.itheima.net/api/login"# 验证码def get_verify_code(self):return requests.get(url=self.url_verify)# 登录def login(self, test_data):# 不用headers 因为json 默认 content-Type : application/jsonreturn requests.post(url=self.url_login, json=test_data)
from api.login import LoginAPI
import unittest
# 调用
class TestContractBusiness(unittest.TestCase):def setUp(self):self.login_api = LoginAPI()def tearDown(self):passdef test01_login_success(self):res_v = self.login_api.get_verify_code()print(res_v.status_code)print(res_v.json())print(res_v.json().get("uuid"))login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})
return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})json headers自动设置Content-Type = application/json
from api.login import LoginAPI
from api.course import CourseAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())
上传文件
import requestsclass ContractAPI:def __init__(self):self.url_upload = "http://kdtx-test.itheima.net/api/common/upload"self.url_add_contract = "http://kdtx-test.itheima.net/api/contract"# 合同上传接口def upload_contract(self, test_data, token):return requests.post(url=self.url_upload, files={"file": test_data}, headers={"Authorization": token})
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()self.contract = ContractAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())# 上传合同成功def test03_upload_contract(self):f = open("../data/test.pdf", "rb")response = self.contract.upload_contract(test_data=f, token=TestContractBusiness.token)print(response.json())
import requestsclass ContractAPI:def __init__(self):self.url_upload = "http://kdtx-test.itheima.net/api/common/upload"self.url_add_contract = "http://kdtx-test.itheima.net/api/contract"# 合同上传接口def upload_contract(self, test_data, token):return requests.post(url=self.url_upload, files={"file": test_data}, headers={"Authorization": token})# 合同新增def add_contract(self, test_data, token):return requests.post(url=self.url_add_contract, json=test_data, headers={"Authorization": token})
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
import unittestclass TestContractBusiness(unittest.TestCase):token = Nonedef setUp(self):self.login_api = LoginAPI()self.course_api = CourseAPI()self.contract = ContractAPI()def tearDown(self):passdef test01_login_success(self):# 获取验证码res_v = self.login_api.get_verify_code()# print(res_v.status_code)print(res_v.json())# 打印uuidprint(res_v.json().get("uuid"))# 登录login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": res_v.json().get("uuid")}res_l = self.login_api.login(test_data=login_data)print(res_l.status_code)print(res_l.json())TestContractBusiness.token = res_l.json().get("token")print(TestContractBusiness.token)# 课程新增成功def test02_add_course(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course_api.add_course(test_data=add_data, token=self.token)print(response.json())# 上传合同成功def test03_upload_contract(self):f = open("../data/test.pdf", "rb")response = self.contract.upload_contract(test_data=f, token=TestContractBusiness.token)print(response.json())def test04_add_contract(self):# contractNo 数据唯一add_data = {"name": "测试888","phone": "13612345678","contractNo": "HT2024070806","subject": "6","courseId": "99","channel": "0","activityId": 77,"fileName": "xxx"}response = self.contract.add_contract(test_data=add_data, token=TestContractBusiness.token)print(response.json())
断言
from api.login import LoginAPI
import unittestclass TestLoginAPI(unittest.TestCase):uuid = Nonedef setUp(self):self.login_api = LoginAPI()response = self.login_api.get_verify_code()TestLoginAPI.uuid = response.json().get("uuid")def tearDown(self):passdef test01_sucess(self):login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)# 预期 实际assert 200 == response.status_codeassert '成功' in response.textassert 200 == response.json().get("code")def test02_without_username(self):login_data = {"username": "","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)print(response.json())# 预期 实际assert 200 == response.status_codeassert '错误' in response.textassert 500 == response.json().get("code")def test03_username_not_exist(self):login_data = {"username": "jack666","password": "HM_2023_test","code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)print(response.json())# 预期 实际assert 200 == response.status_codeassert '错误' in response.textassert 500 == response.json().get("code")
参数化
数据准备:
{"login_001":{"username": "admin", "password": "HM_2023_test", "status": 200,"message": "成功","code": 200},"login_002":{"username": "", "password": "HM_2023_test", "status": 200,"message": "错误","code": 500},"login_003":{"username": "jack666", "password": "HM_2023_test", "status": 200,"message": "错误","code": 500}
}
读入数据代码
import jsondef read_json(filename):filepath = "../data/" + filenamewith open(filepath, "r", encoding="utf-8") as f:return json.load(f)
scripts:
from api.login import LoginAPI
import unittest
from parameterized import parameterized
from common.read_json import read_jsondef get_data():data = read_json("login_data.json")list1 = []for i in data.values():list1.append((i.get("username"),i.get("password"),i.get("status"),i.get("message"),i.get("code")))return list1class TestLoginAPI(unittest.TestCase):uuid = Nonedef setUp(self):self.login_api = LoginAPI()response = self.login_api.get_verify_code()TestLoginAPI.uuid = response.json().get("uuid")def tearDown(self):pass@parameterized.expand(get_data())def test_login(self,username,password,status,message,code):login_data = {"username": username,"password": password,"code": 2,"uuid": TestLoginAPI.uuid}response = self.login_api.login(test_data=login_data)# 预期 实际assert status == response.status_codeassert message in response.textassert code == response.json().get("code")
如何测试未登录
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestAddCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestAddCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_success(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course.add_course(test_data=add_data, token=TestAddCourseAPI.token)assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")# 如何测试未登录def test02_fail(self):add_data = {"name": "测试开发提升课", "subject": "6", "price": 899, "applicablePerson": "2", "info": "测试开发提升课"}response = self.course.add_course(test_data=add_data, token="xxx")assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestSelectCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestSelectCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_select_success(self):response = self.course.select_course(test_data="?name=测试开发提升课01",token=TestSelectCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_select_fail(self):response = self.course.select_course(test_data="?name=测试开发提升课01", token="xxx")print(response.json())assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})def update_course(self,test_data,token):return requests.put(url=self.url_add_course, json=test_data, headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestUpdateCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestUpdateCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_update_success(self):update_data = {"id": "23056458704355861", "name": "测试开发提升课", "subject": "6", "price": 899,"applicablePerson": "2", "info": "测试开发提升课"}response = self.course.update_course(test_data=update_data,token=TestUpdateCourseAPI.token)assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_update_fail(self):update_data = {"id": "23056458704355861", "name": "测试开发提升课", "subject": "6", "price": 899,"applicablePerson": "2", "info": "测试开发提升课"}response = self.course.update_course(test_data=update_data,token="xxxx")assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
路径参数时
import requestsclass CourseAPI:def __init__(self):self.url_add_course = "http://kdtx-test.itheima.net/api/clues/course"self.url_select_course = "http://kdtx-test.itheima.net/api/clues/course/list"def add_course(self, test_data, token):return requests.post(url=self.url_add_course, json=test_data, headers={"Authorization": token})def select_course(self, test_data, token):return requests.get(url=self.url_select_course + "/{}".format(test_data), headers={"Authorization": token})def update_course(self,test_data,token):return requests.put(url=self.url_add_course, json=test_data, headers={"Authorization": token})def delete_course(self,course_id, token):return requests.delete(url=self.url_add_course + "/{}".format(course_id), headers={"Authorization": token})
import unittest
from api.login import LoginAPI
from api.course import CourseAPIclass TestDeleteCourseAPI(unittest.TestCase):token = Nonedef setUp(self):self.login = LoginAPI()self.course = CourseAPI()res_v = self.login.get_verify_code()uuid = res_v.json().get("uuid")login_data = {"username": "admin","password": "HM_2023_test","code": 2,"uuid": uuid}res_l = self.login.login(login_data)TestDeleteCourseAPI.token = res_l.json().get("token")def tearDown(self):passdef test01_delete_success(self):response = self.course.delete_course(course_id=23056458704355867,token=TestDeleteCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "成功" in response.textassert 200 == response.json().get("code")def test02_delete_id_not_exist(self):response = self.course.delete_course(course_id=987654321,token=TestDeleteCourseAPI.token)print(response.json())assert 200 == response.status_codeassert "失败" in response.textassert 500 == response.json().get("code")def test03_delete_fail(self):response = self.course.delete_course(course_id=23056458704355868,token="xxxx")print(response.json())assert 200 == response.status_codeassert "认证失败" in response.textassert 401 == response.json().get("code")
config:
# 存放被测项目基本信息 如URL地址等
import os
BASE_URL = "http://kdtx-test.itheima.net"BASE_PATH = os.path.dirname(__file__)
print(BASE_PATH)
测试报告
from common.HTMLTestRunner import HTMLTestRunner
import unittest
import time
import config
suite = unittest.defaultTestLoader.discover(config.BASE_PATH + "/script/","test*.py")with open(config.BASE_PATH + "/report/{}.html".format(time.strftime("%Y_%m_%d %H_%M_%S")),"wb") as f:HTMLTestRunner(stream=f).run(suite)