1.config文件
在Python接口自动化测试框架中,
config
文件扮演着至关重要的角色。它主要用于存储和管理测试过程中需要的配置信息,如数据库连接信息、API的URL、测试环境(如开发环境、测试环境、生产环境)的切换、日志配置等。通过将这些配置信息集中管理,可以使得测试脚本更加灵活和易于维护。
# 存放被测试项目的基本信息,如url地址等
import os
# 设置项目环境名
BASE_URL = "http://kdtx-test.itheima.net"
# 获取项目根路径
BASE_PATH = os.path.dirname(__file__)
print(BASE_PATH)
以下是test09_login_params.py文件:
import config
from api.login import LoginAPI
import pytest
import json
# # 测试数据
# test_data = {
# ("admin", "HM_2023_test", 200, "成功", 200),
# ("", "HM_2023_test", 200, "错误", 500),
# ("duaygefi", "HM_2023_test", 200, "错误", 500)
# }
# 读取json文件
def build_data(json_file):
test_data = []
with open(json_file, "r",encoding="utf-8") as f:
json_file = json.load(f)
for case_data in json_file:
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
class TestLoginAPI:
# 类属性
uuid = None
def setup_method(self):
self.login_api = LoginAPI()
# 获取验证码
response = self.login_api.get_verify_code()
print(response.json())
# 将验证码给类属性uuid
TestLoginAPI.uuid = response.json().get("uuid")
print(TestLoginAPI.uuid)
def teardown(self):
pass
# 登录成功(参数化)
@pytest.mark.parametrize("username,password,status,message,code", build_data(json_file=config.BASE_PATH + "/data/login.json"))
def test01_success(self, username, password, status, message, code):
login_data = {
"username": username,
"password": password,
"code": "2",
"uuid": TestLoginAPI.uuid
}
login_header_data = {
"Content-Type": "application/json"
}
response = self.login_api.login(test_header=login_header_data, test_data=login_data)
assert status == response.status_code
assert message in response.text
assert code == response.json().get("code")
# 登录失败(用户名为空)
def test02_username_null(self):
login_data = {
"username": "",
"password": "HM_2023_test",
"code": "2",
"uuid": TestLoginAPI.uuid
}
login_header_data = {
"Content-Type": "application/json"
}
response = self.login_api.login(test_header=login_header_data, test_data=login_data)
assert 200 == response.status_code
assert '错误' in response.text
assert 500 == response.json().get("code")
# 登录失败(未登录)
def test03_username_not_exist(self):
login_data = {
"username": "adminryctv",
"password": "HM_2023_test",
"code": "2",
"uuid": TestLoginAPI.uuid
}
login_header_data = {
"Content-Type": "application/json"
}
response = self.login_api.login(test_header=login_header_data, test_data=login_data)
assert 200 == response.status_code
assert '错误' in response.text
assert 500 == response.json().get("code")
2.Allure测试报告文件
在终端输入pip install allure安装Allure
输入allure --version可查看allure的版本
然后还需要pip install allure-pytest来与pytest对接
2.1设置pytest.ini文件
[pytest]
addopts=-s --alluredir report
testpaths=./script
python_files=test*.py
python_classes=Test*
python_functions=test*
2.2执行allure
在终端输入allure serve report
点击Show all即可查看测试点