优化各个用例的登录逻辑,避免不同用例重复使用requests.session()方法,替换前置处理setup_class()方法
1、在用例层case创建conftest.py(固定写法)

2、/case/conftest.py
import pytest, requests
@pytest.fixture(scope="class")
def login():
s = requests.session()
login = s.request("POST", url="http://localhost/userlogin",
data={"username": "test01", "password": "123456"})
assert login.json().get("msg") == "login-pass"
yield s # 将登录后的 session 对象传递给测试用例
3、/case/test_student.py
import pytest
import requests, allure
from PytestApiExcel.config.config import Config
from PytestApiExcel.common.readcases import ReadCase
from PytestApiExcel.common.MyRequest import MyRequest
class TestStudent(object):
# 调用文件读取的方法,获取excel文件中,sheet页名为login的文件用例数据
case_data = ReadCase(Config.project_path + "/datas/files/test_case.xlsx").read_sheetname("student")
# 减少登录次数
# def setup_class(self):
# self.r = requests.session()
# login_r = self.r.request("POST", url="http://localhost/userlogin",
# data={"username": "test01", "password": "123456"})
# assert login_r.json().get("msg") == "login-pass"
# 通过数据参数化的方式,用“case”进行接收
@pytest.mark.parametrize("case", case_data)
def test_student(self, case,login): ## 接收测试数据和 fixture中的login方法
response = MyRequest.request(case,request_session=login) # 使用 fixture 的 session
3767

被折叠的 条评论
为什么被折叠?



