Python接口自動化測試是一種使用Python編程語言來編寫腳本以自動執(zhí)行針對應(yīng)用程序接口(APIs)的測試過程。這種測試方法專注于檢查系統(tǒng)的不同組件或服務(wù)之間的交互,確保它們按照預(yù)期規(guī)范進行通信,而不涉及用戶界面(UI)的驗證。
目錄
一、接口測試基礎(chǔ)
二、工具實現(xiàn)接口測試
2.1、postman接口測試
2.2、postman自動關(guān)聯(lián)
2.3、postman批量執(zhí)行
2.4、登錄單接口測試
2.5、postman斷言
2.6、postman參數(shù)化
2.7、課程增刪改查測試
三、代碼實現(xiàn)接口測試
2.1、圖片驗證碼獲取及登錄自動化
2.2、課程新增代碼自動化
2.3、合同上傳與新增接口自動化
2.4、登錄單接口自動化測試
2.5、登錄單接口自動化-數(shù)據(jù)驅(qū)動實現(xiàn)
2.6、課程添加自動化
2.7、課程查詢接口自動化
2.8、課程修改接口自動化
2.9、課程刪除接口自動化
2.10、項目配置文件config
2.11、allure生成測試報告
一、接口測試基礎(chǔ)
URL的組成=協(xié)議+服務(wù)器地址+端口號+資源路徑+參數(shù)
Http請求:請求行+請求頭+請求體
請求行=請求類型+資源地址+版本協(xié)議
請求頭(鍵值對類型)=請求數(shù)據(jù)類型等
請求體=請求的數(shù)據(jù),一般使用Json類型
Http響應(yīng):狀態(tài)行+響應(yīng)頭
狀態(tài)行=協(xié)議版本號+響應(yīng)狀態(tài)碼+響應(yīng)狀態(tài)說明
響應(yīng)頭=響應(yīng)服務(wù)器+響應(yīng)時間+響應(yīng)類型+響應(yīng)內(nèi)容長度
響應(yīng)體=響應(yīng)的內(nèi)容,一般是json串的形式
二、工具實現(xiàn)接口測試
2.1、postman接口測試
1.使用postman發(fā)送GET請求:查詢uuid
2.使用postman發(fā)送POST請求:根據(jù)uuid進行登錄請求
2.2、postman自動關(guān)聯(lián)
有多個接口請求,需要將一個接口的返回的結(jié)果保存為變量,另外一個接口請求的時候使用保存變量。
1.首先創(chuàng)建一個環(huán)境變量:并命名為FAT。
2.在第一個接口中設(shè)置環(huán)境變量,并將第一個接口請求的參數(shù)設(shè)置到環(huán)境變量中去。
3.在第二個接口的請求參數(shù)中直接動態(tài)引用從第一個接口中獲取的動態(tài)變量值。
4.在登錄接口中獲取響應(yīng)得到的token,存儲為環(huán)境變量。
5.在新增課程的接口中,引用登錄接口存儲的token變量。
6.合同上傳接口,需要獲取登錄接口保存的token環(huán)境變量。
7.在合同上傳接口的請求體里設(shè)置待上傳的合同文件。
8.需要在合同上傳接口中將響應(yīng)得到的fileName字段保存為環(huán)境變量。
9.在添加合同接口中使用{{fileName}}引用合同上傳接口中保存的環(huán)境變量。
10.通過手機號查詢合同信息。
2.3、postman批量執(zhí)行
在接口自動化測試文件夾中選擇run,然后點擊如下箭頭所示的Run自動化測試即可實現(xiàn)接口的批量執(zhí)行。
2.4、登錄單接口測試
1.可以創(chuàng)建一個文件夾,在文件夾下建立測試用例請求,進行一個接口的所有用例的測試。
2.5、postman斷言
可以在postman中設(shè)置斷言,Tests中斷言函數(shù),通過斷言函數(shù)對響應(yīng)結(jié)果進行斷言。
2.6、postman參數(shù)化
設(shè)置文件并加載,然后使用動態(tài)參數(shù){{變量名}}獲取文件中的字段。
2.7、課程增刪改查測試
由于增刪改查的思路都是一致的,設(shè)置請求url,請求頭以及請求體即可完成測試,在一個課程添加文件夾下可以測試與管理多個用例。
三、代碼實現(xiàn)接口測試
接口自動化測試流程:1.選取自動化測試用例 2.搭建自動化測試環(huán)境 3.搭建自動化測試框架 4.代碼實現(xiàn)自動化 5.輸出測試報告 6.實現(xiàn)持續(xù)集成
核心技術(shù):python語言+pytest框架+requests框架
自動化測試框架的目錄結(jié)構(gòu):
2.1、圖片驗證碼獲取及登錄自動化
第一步,獲取圖片驗證碼:
# 獲取圖片驗證碼
# 1.導(dǎo)包
import requests
# 2.發(fā)送請求
response = requests.get(url="http://kdtx-test.itheima.net/api/captchaImage")
# 3.查看響應(yīng)
print(response.status_code)
print(response.text)
第二步:根據(jù)獲取的驗證碼uuid發(fā)送登錄請求
# 登錄
# 1.導(dǎo)包
import requests
# 2.發(fā)送請求
url = "http://kdtx-test.itheima.net/api/login"
header_data = {
"Content-Type": "application/json"
}
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": "98648849e1214e67bd7c9d4af396ab69"
}
try:
response = requests.post(url=url,headers=header_data,json=login_data)
except Exception as e:
print("異常:", e)
finally:
pass
# 3.獲取響應(yīng)結(jié)果
print(response.status_code)
print(response.json())
上述的代碼并沒有合理的實現(xiàn)api的封裝與調(diào)用,下面將登錄過程封裝到api包下的loginAPI類中,再后續(xù)的測試中直接實例化登錄類,并調(diào)用相應(yīng)的方法即可實現(xiàn)登錄。
# 依據(jù)接口文檔,封接口信息,需要使用的測試數(shù)據(jù)從測試用例傳遞
# 接口方法被調(diào)用需要返回對應(yīng)的響應(yīng)結(jié)果
# 1.導(dǎo)包
import requests
# 2.創(chuàng)建接口類
class loginAPI:
# 初始化
def __init__(self):
# 指定url
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):
return requests.post(self.url_login, json=test_data)
在具體的測試類中調(diào)用封裝的api進行獲取驗證碼以及登錄。
# 1.導(dǎo)包
from api.login import loginAPI
# 2.創(chuàng)建測試類
class TestContractBusiness:
# 2.1 前置處理
def setup(self):
# 實例化接口對象
self.login_api = loginAPI()
# 2.2 后置處理
def teardown(self):
pass
# 01.登錄成功
def test01_login_success(self):
# 獲取驗證碼
res_first = self.login_api.get_verify_code()
print(res_first.status_code)
print(res_first.json())
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res_first.json().get("uuid")
}
res_second = self.login_api.login(login_data)
print(res_second.status_code)
print(res_second.json())
if __name__ == '__main__':
test = TestContractBusiness()
test.setup()
test.test01_login_success()
2.2、課程新增代碼自動化
首先封裝課程新增接口類。
# 課程接口的封裝
# 導(dǎo)包
import requests
# 創(chuàng)建接口類
class courseAPI:
# 初始化
def __init__(self):
self.add_course_url = "http://kdtx-test.itheima.net/api/clues/course/"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
然后在測試類中,定義課程新增測試方法。
# 1.導(dǎo)包
from api.login import loginAPI
from api.course import courseAPI
# 2.創(chuàng)建測試類
class TestContractBusiness:
# 2.1 前置處理
def setup(self):
# 實例化接口對象
self.login_api = loginAPI()
self.course_api = courseAPI()
self.token = None
# 2.2 后置處理
def teardown(self):
pass
# 01.登錄成功
def test01_login_success(self):
# 獲取驗證碼
res_first = self.login_api.get_verify_code()
print(res_first.status_code)
print(res_first.json())
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res_first.json().get("uuid")
}
res_second = self.login_api.login(login_data)
print(res_second.status_code)
print(res_second.json())
TestContractBusiness.token = res_second.json().get("token")
# 新增課程成功
def test02_add_course(self):
add_data = {
"name": "測試開發(fā)課程",
"subject": "6",
"price": "344",
"applicablePerson": "2"
}
res_third = self.course_api.add_course(test_data=add_data, token=TestContractBusiness.token)
print(res_third.status_code)
print(res_third.json())
if __name__ == '__main__':
test = TestContractBusiness()
test.setup()
test.test01_login_success()
test.test02_add_course()
2.3、合同上傳與新增接口自動化
首先封裝合同上傳與新增接口,如下:
# 1.導(dǎo)包
import requests
# 2.定義合同接口類
class ContractAPI:
# 初始化
def __init__(self):
self.url_upload = "http://kdtx-test.itheima.net/api/common/upload"
self.add_contract_url = "http://kdtx-test.itheima.net/api/contract"
# 合同上傳接口
def upload_abstract(self, test_data, token):
return requests.post(url=self.url_upload, files={"file": test_data}, headers={"Authorization": token})
# 合同新增接口
def add_abstract(self, test_data, token):
return requests.post(url=self.add_contract_url, json=test_data, headers={"Authorization": token})
編寫合同上傳和合同新增測試腳本:
?
# 1.導(dǎo)包
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
# 2.創(chuàng)建測試類
class TestContractBusiness:
# 2.1 前置處理
token = None
file_name = None
def setup(self):
# 實例化接口對象
self.login_api = LoginAPI()
self.course_api = CourseAPI()
self.contract_api = ContractAPI()
# 2.2 后置處理
def teardown(self):
pass
# 01.登錄成功
def test01_login_success(self):
# 獲取驗證碼
res_first = self.login_api.get_verify_code()
print(res_first.status_code)
print(res_first.json())
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res_first.json().get("uuid")
}
res_second = self.login_api.login(login_data)
print(res_second.status_code)
print(res_second.json())
TestContractBusiness.token = res_second.json().get("token")
# 02.新增課程成功
def test02_add_course(self):
add_data = {
"name": "測試開發(fā)課程1",
"subject": "6",
"price": "344",
"applicablePerson": "2"
}
res_third = self.course_api.add_course(test_data=add_data, token=TestContractBusiness.token)
print(res_third.status_code)
print(res_third.json())
# 03.合同上傳接口
def test03_upload_abstract(self):
file = open("../data/test_file.txt", "rb")
response = self.contract_api.upload_abstract(test_data=file, token=TestContractBusiness.token)
print(response.status_code)
print(response.json())
TestContractBusiness.file_name = response.json().get("fileName")
# 04.合同新增接口
def test04_add_abstract(self):
add_data = {
"name": "測試",
"phone": "13827648970",
"contractNo": "HT20240ww3",
"subject": "6",
"courseId": "99",
"channel": "0",
"activityId" : 77,
"fileName": TestContractBusiness.file_name
}
response = self.contract_api.add_abstract(test_data=add_data, token=TestContractBusiness.token)
print(response.status_code)
print(response.json())
if __name__ == '__main__':
test = TestContractBusiness()
test.setup()
test.test01_login_success()
test.test02_add_course()
test.test03_upload_abstract()
test.test04_add_abstract()
2.4、登錄單接口自動化測試
首先封裝獲取驗證碼與登錄接口。
# 依據(jù)接口文檔,封接口信息,需要使用的測試數(shù)據(jù)從測試用例傳遞
# 接口方法被調(diào)用需要返回對應(yīng)的響應(yīng)結(jié)果
# 1.導(dǎo)包
import requests
# 2.創(chuàng)建接口類
class LoginAPI:
# 初始化
def __init__(self):
# 指定url
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):
return requests.post(self.url_login, json=test_data)
編寫等于測試用例腳本,并對響應(yīng)結(jié)果做校驗。
# 1.導(dǎo)包
from api.login import LoginAPI
# 2.創(chuàng)建測試類
class TestLoginAPI:
uuid = None
# 前置處理
def setup(self):
# 實例化
self.login_api = LoginAPI()
# 獲取驗證碼
response = self.login_api.get_verify_code()
# 獲取驗證碼中的uuid
TestLoginAPI.uuid = response.json().get("uuid")
# 后置處理
def teardown(self):
pass
# 登錄成功
def login_success(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_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert '成功' in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert 200 == response.json().get("code")
# 登錄成功
# 登錄失敗
def login_fail(self):
login_data = {
"username": "",
"password": "HM_2023_test",
"code": "2",
"uuid": TestLoginAPI.uuid
}
response = self.login_api.login(test_data=login_data)
# 斷言驗證碼
assert 200 == response.status_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert '錯誤' in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert 500 == response.json().get("code")
if __name__ == '__main__':
test_login = TestLoginAPI()
test_login.setup()
test_login.login_success()
test_login.setup()
test_login.login_fail()
2.5、登錄單接口自動化-數(shù)據(jù)驅(qū)動實現(xiàn)
數(shù)據(jù)驅(qū)動:以測試數(shù)據(jù)驅(qū)動腳本執(zhí)行,維護焦點從腳本轉(zhuǎn)向測試數(shù)據(jù)的一種自動化測試用例設(shè)計模式。
方式1:直接定義測試數(shù)據(jù),然后使用pytest框架進行參數(shù)化,實現(xiàn)數(shù)據(jù)驅(qū)動。
# 1.導(dǎo)包
from api.login import LoginAPI
import pytest
# 測試數(shù)據(jù)
test_data = [("admin", "HM_2023_test", 200, '成功', 200),
("", "HM_2023_test", 200, '錯誤', 500)]
# 2.創(chuàng)建測試類
class TestLoginAPI:
uuid = None
# 前置處理
def setup(self):
# 實例化
self.login_api = LoginAPI()
# 獲取驗證碼
response = self.login_api.get_verify_code()
# 獲取驗證碼中的uuid
TestLoginAPI.uuid = response.json().get("uuid")
# 后置處理
def teardown(self):
pass
# 登錄成功
@pytest.mark.parametrize("username, password, status, message, code", test_data)
def test_login_success(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_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
# 登錄成功
# 登錄失敗
@pytest.mark.parametrize("username, password, status, message, code", test_data)
def test_login_fail(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_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
方式2:
首先在data文件夾定義:login.json文件用于測試數(shù)據(jù)。
[
{
"username": "admin",
"password": "HM_2023_test",
"status": 200,
"message": "成功",
"code": 200
},
{
"username": "",
"password": "HM_2023_test",
"status": 200,
"message": "錯誤",
"code": 500
}
]
使用pytest做參數(shù)化,完成數(shù)據(jù)驅(qū)動測試。
# 1.導(dǎo)包
from api.login import LoginAPI
import pytest
import json
# 讀取json文件
def build_data(json_file):
# 定義空列表
test_data = []
# 打開json
with open(json_file, "r", encoding='utf-8') as file:
# 加載json數(shù)據(jù)
json_data = json.load(file)
# 循環(huán)遍歷測試數(shù)據(jù)
for case_data in json_data:
# 解析數(shù)據(jù)
username = case_data.get("username")
password = case_data.get("password")
status = case_data.get("status")
message = case_data.get("message")
code = case_data.get("code")
test_data.append((username, password, status, message, code))
return test_data
# 2.創(chuàng)建測試類
class TestLogin:
uuid = None
# 前置處理
def setup(self):
# 實例化
self.login_api = LoginAPI()
# 獲取驗證碼
response = self.login_api.get_verify_code()
# 獲取驗證碼中的uuid
TestLogin.uuid = response.json().get("uuid")
# 后置處理
def teardown(self):
pass
# 登錄成功
@pytest.mark.parametrize("username, password, status, message, code", build_data(json_file="../data/login.json"))
def test_login__success(self, username, password, status, message, code):
login_data = {
"username": username,
"password": password,
"code": "2",
"uuid": TestLogin.uuid
}
response = self.login_api.login(test_data=login_data)
# 斷言驗證碼
assert status == response.status_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
# 登錄成功
# 登錄失敗
@pytest.mark.parametrize("username, password, status, message, code", build_data(json_file="../data/login.json"))
def test_login_fail(self, username, password, status, message, code):
login_data = {
"username": username,
"password": password,
"code": "2",
"uuid": TestLogin.uuid
}
response = self.login_api.login(test_data=login_data)
# 斷言驗證碼
assert status == response.status_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
正常pychram調(diào)試的時候方法和類是沒有綠色三角形的,即不能單獨調(diào)試,需要自定義main函數(shù)進行實例并調(diào)用方法調(diào)試,但是可以將類與方法加載到pytest框架中即可單獨調(diào)試函數(shù)和類。
但是需要注意2點:
第一:需要設(shè)置測試框架使用pytest
第二,類和方法的命名要符合pytest框架規(guī)范,pytest才能識別:
類需要以Test開頭,方法需要以test_開頭,另外python文件名需要以test_
開頭,或_test
結(jié)尾
2.6、課程添加自動化
首先需要在api目錄下封裝登錄接口與課程上傳接口。
# 1.導(dǎo)包
import requests
# 2.創(chuàng)建接口類
class LoginAPI:
# 初始化
def __init__(self):
# 指定url
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):
return requests.post(self.url_login, json=test_data)
# 導(dǎo)包
import requests
# 創(chuàng)建接口類
class CourseAPI:
# 初始化
def __init__(self):
self.add_course_url = "http://kdtx-test.itheima.net/api/clues/course/"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
然后編寫測試腳本課程上傳并斷言結(jié)果。
# 導(dǎo)包
from api.login import LoginAPI
from api.course import CourseAPI
# 課程添加類
class TestAddCourseAPI:
token = None
def setup(self):
# 初始化接口
self.api_login = LoginAPI()
self.api_course = CourseAPI()
# 獲取驗證碼
res = self.api_login.get_verify_code()
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res.json().get("uuid")
}
response = self.api_login.login(test_data=login_data)
TestAddCourseAPI.token = response.json().get("token")
def teardown(self):
pass
# 課程添加成功
def test_add_course_success(self):
add_data = {
"name": "測試開發(fā)課程",
"subject": "6",
"price": "344",
"applicablePerson": "2"
}
response = self.api_course.add_course(test_data=add_data, token=TestAddCourseAPI.token)
# 斷言響應(yīng)狀態(tài)碼
assert 200 == response.status_code
# 斷言message
assert '成功' in response.text
# 斷言code
assert 200 == response.json().get("code")
2.7、課程查詢接口自動化
首先封裝查詢課程接口api,同時需要封裝登錄接口api。
# 導(dǎo)包
import requests
# 創(chuàng)建接口類
class CourseAPI:
# 初始化
def __init__(self):
self.add_course_url = "http://kdtx-test.itheima.net/api/clues/course/"
self.select_course_url = "http://kdtx-test.itheima.net/api/clues/course/list"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 查詢課程列表
def select_course(self, test_data, token):
return requests.get(url=self.select_course_url + f"{test_data}", headers={"Authorization": token})
使用pytest框架調(diào)用封裝的課程查詢接口實現(xiàn)課程查詢自動化。
# 導(dǎo)包
from api.login import LoginAPI
from api.course import CourseAPI
# 課程添加類
class TestAddCourseAPI:
token = None
def setup(self):
# 初始化接口
self.api_login = LoginAPI()
self.api_course = CourseAPI()
# 獲取驗證碼
res = self.api_login.get_verify_code()
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res.json().get("uuid")
}
response = self.api_login.login(test_data=login_data)
TestAddCourseAPI.token = response.json().get("token")
def teardown(self):
pass
# 課程查詢成功
def test_select_course_success(self):
response = self.api_course.select_course(test_data="?name=測試開發(fā)提升課", token=TestAddCourseAPI.token)
# 斷言
assert 200 == response.status_code
assert '成功' in response.text
assert 200 == response.json().get("code")
# 課程查詢失敗
def test_select_course_fail(self):
response = self.api_course.select_course(test_data="?name=測試開發(fā)提升課", token="***")
# 斷言
assert 200 == response.status_code
assert '認證失敗' in response.text
assert 401 == response.json().get("code")
2.8、課程修改接口自動化
首先在api包下先封裝課程修改接口,如下:
# 導(dǎo)包
import requests
# 創(chuàng)建接口類
class CourseAPI:
# 初始化
def __init__(self):
self.add_course_url = "http://kdtx-test.itheima.net/api/clues/course/"
self.select_course_url = "http://kdtx-test.itheima.net/api/clues/course/list"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 查詢課程列表
def select_course(self, test_data, token):
return requests.get(url=self.select_course_url + f"{test_data}", headers={"Authorization": token})
# 修改課程
def update_course(self, test_data, token):
return requests.put(url=self.add_course_url, json=test_data, headers={"Authorization": token})
在script包下編寫測試腳本,測試課程修改接口:
?
# 導(dǎo)包
from api.login import LoginAPI
from api.course import CourseAPI
# 課程添加類
class TestUpdateCourseAPI:
token = None
def setup(self):
# 初始化接口
self.api_login = LoginAPI()
self.api_course = CourseAPI()
# 獲取驗證碼
res = self.api_login.get_verify_code()
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res.json().get("uuid")
}
response = self.api_login.login(test_data=login_data)
TestUpdateCourseAPI.token = response.json().get("token")
def teardown(self):
pass
# 課程修改成功
def test_update_course_success(self):
update_data = {
"id": "109",
"name": "測試接口001",
"subject": "6",
"price": 999,
"applicablePerson": "2",
"info": "課程介紹003"
}
response = self.api_course.update_course(test_data=update_data, token=TestUpdateCourseAPI.token)
# 斷言
assert 200 == response.status_code
assert '成功' in response.text
assert 200 == response.json().get("code")
# 課程修改失敗
def test_update_course_fail(self):
update_data = {
"id": "109",
"name": "測試接口001",
"subject": "6",
"price": 999,
"applicablePerson": "2",
"info": "課程介紹003"
}
response = self.api_course.update_course(test_data=update_data, token="***")
# 斷言
assert 200 == response.status_code
assert '認證失敗' in response.text
assert 401 == response.json().get("code")
2.9、課程刪除接口自動化
首先封裝刪除課程的接口,具體如下:
# 課程接口的封裝
# 導(dǎo)包
import requests
# 創(chuàng)建接口類
class CourseAPI:
# 初始化
def __init__(self):
self.add_course_url = "http://kdtx-test.itheima.net/api/clues/course/"
self.select_course_url = "http://kdtx-test.itheima.net/api/clues/course/list"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 查詢課程列表
def select_course(self, test_data, token):
return requests.get(url=self.select_course_url + f"{test_data}", headers={"Authorization": token})
# 修改課程
def update_course(self, test_data, token):
return requests.put(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 刪除課程
def delete_course(self, course_id, token):
return requests.delete(self.add_course_url + f"/{course_id}", headers={"Authorization": token})
然后編寫測試腳本對課程刪除接口進行測試,并斷言測試結(jié)果。
# 導(dǎo)包
from api.login import LoginAPI
from api.course import CourseAPI
# 課程刪除類
class TestDeleteCourseAPI:
token = None
def setup(self):
# 初始化接口
self.api_login = LoginAPI()
self.api_course = CourseAPI()
# 獲取驗證碼
res = self.api_login.get_verify_code()
# 登錄
login_data = {
"username": "admin",
"password": "HM_2023_test",
"code": "2",
"uuid": res.json().get("uuid")
}
response = self.api_login.login(test_data=login_data)
TestDeleteCourseAPI.token = response.json().get("token")
def teardown(self):
pass
# 課程刪除成功
def test_delete_course_success(self):
response = self.api_course.delete_course(course_id=109, token=TestDeleteCourseAPI.token)
# 斷言
assert 200 == response.status_code
assert '成功' in response.text
assert 200 == response.json().get("code")
# 課程刪除失敗(課程id不存在)
def test_delete_course_fail_id(self):
response = self.api_course.delete_course(course_id=1000000000, token=TestDeleteCourseAPI.token)
# 斷言
assert 200 == response.status_code
assert '操作失敗' in response.text
assert 500 == response.json().get("code")
# 課程刪除失敗(未登錄)
def test_delete_course_fail_login(self):
response = self.api_course.delete_course(course_id=110, token="***")
# 斷言
assert 200 == response.status_code
assert '認證失敗' in response.text
assert 401 == response.json().get("code")
2.10、項目配置文件config
在主項目的根目錄下定義一個config1.py文件定義url的共用路徑與當前根目錄的地址。
# 配置類
import os
# 設(shè)置項目環(huán)境域名
BASE_URL = "http://kdtx-test.itheima.net"
# 項目根路徑
BASE_PATH = os.path.dirname(__file__)
接口封裝類中換成BASE_URL拼接的形式。
# 導(dǎo)包
import requests
import config1
# 創(chuàng)建接口類
class CourseAPI:
# 初始化
def __init__(self):
self.add_course_url = config1.BASE_URL + "/api/clues/course/"
self.select_course_url = config1.BASE_URL + "/api/clues/course/list"
# 課程添加
def add_course(self, test_data, token):
return requests.post(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 查詢課程列表
def select_course(self, test_data, token):
return requests.get(url=self.select_course_url + f"{test_data}", headers={"Authorization": token})
# 修改課程
def update_course(self, test_data, token):
return requests.put(url=self.add_course_url, json=test_data, headers={"Authorization": token})
# 刪除課程
def delete_course(self, course_id, token):
return requests.delete(self.add_course_url + f"/{course_id}", headers={"Authorization": token})
具體的路徑中換成BASE_PATH拼接的形式。
# 1.導(dǎo)包
from api.login import LoginAPI
import pytest
import json
import config1
# 讀取json文件
def build_data(json_file):
# 定義空列表
test_data = []
# 打開json
with open(json_file, "r", encoding='utf-8') as file:
# 加載json數(shù)據(jù)
json_data = json.load(file)
# 循環(huán)遍歷測試數(shù)據(jù)
for case_data in json_data:
# 解析數(shù)據(jù)
username = case_data.get("username")
password = case_data.get("password")
status = case_data.get("status")
message = case_data.get("message")
code = case_data.get("code")
test_data.append((username, password, status, message, code))
return test_data
# 2.創(chuàng)建測試類
class TestLogin:
uuid = None
# 前置處理
def setup(self):
# 實例化
self.login_api = LoginAPI()
# 獲取驗證碼
response = self.login_api.get_verify_code()
# 獲取驗證碼中的uuid
TestLogin.uuid = response.json().get("uuid")
# 后置處理
def teardown(self):
pass
# 登錄成功
@pytest.mark.parametrize("username, password, status, message, code", build_data(json_file=config1.BASE_PATH + "/data/login.json"))
def test_login__success(self, username, password, status, message, code):
login_data = {
"username": username,
"password": password,
"code": "2",
"uuid": TestLogin.uuid
}
response = self.login_api.login(test_data=login_data)
# 斷言驗證碼
assert status == response.status_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
# 登錄成功
# 登錄失敗
@pytest.mark.parametrize("username, password, status, message, code", build_data(json_file=config1.BASE_PATH + "/data/login.json"))
def test_login_fail(self, username, password, status, message, code):
login_data = {
"username": username,
"password": password,
"code": "2",
"uuid": TestLogin.uuid
}
response = self.login_api.login(test_data=login_data)
# 斷言驗證碼
assert status == response.status_code
# 斷言響應(yīng)數(shù)據(jù)包含成功
assert message in response.text
# 斷言響應(yīng)json數(shù)據(jù)中的code值
assert code == response.json().get("code")
2.11、allure生成測試報告
使用pip指令安裝allure:pip install allure-pytest
然后下載并配置allure,下載地址:Central Repository: io/qameta/allure/allure-commandline
下載安裝解壓到指定目錄,并將allure的bin目錄路徑配置到環(huán)境變量中系統(tǒng)變量的path。
配置完成后使用allure --version命令檢測。
在pytest.ins文件中配置執(zhí)行信息。包含報告輸出信息、測試腳本路徑、測試類、測試函數(shù)等
[pytest]
addopts =-s --alluredir report
testpaths =./script
python_files = test*.py
python_classes = Test*
python_functions = test*
在pychram的終端中使用命令:pytest 批量執(zhí)行測試用例
使用allure serve report 命令查看測試報告:文章來源:http://www.zghlxwxcb.cn/news/detail-854805.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-854805.html
到了這里,關(guān)于Python接口自動化測試-篇1(postman+requests+pytest+allure)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!