python+requests+pytest+allure+log+yaml问答题

python如何发起请求

import requests

if __name__ == '__main__':
    url = "https://www.baidu.com/"
    res = requests.get(url)
    res.encoding = 'utf-8'  #解决中文乱码
    print(res.text)

响应结果如何进行校验

import requests

if __name__ == '__main__':
    url = "https://www.baidu.com/"
    res = requests.get(url)
    res.encoding = 'utf-8'  #解决中文乱码
    print(res.text)
    #assert xx :判断 xx 为真、assert not xx :判断 xx 不为真、assert a in b :判断 b 包含 a、assert a == b :判断 a 等于 b、assert a != b :判断 a 不等于 b
    assert "百度" in res.text
    assert "测试" not in res.text

引入pytest进行测试

import requests
import pytest

def test_baidu():
    url = "https://www.baidu.com/"
    res = requests.get(url)
    res.encoding = 'utf-8'  #解决中文乱码
    print(res.text)
    assert "百度" in res.text

if __name__ == '__main__':
    pytest.main(['-s','test_baidu'])

引入yaml文件进行维护测试用例

说明:先配置yaml文件,再读取文件信息作为参数

def open_login_yaml():
    with open("E:\\工作\\测试\\项目代码\\测试专用\\ApiTest\\testcase\loginCase.yaml", encoding="utf-8") as yaml_file:
        data = yaml.safe_load(yaml_file)
    return data

使用pytest的parametrize装饰器,作为数据驱动。

@allure.feature("学生端上课")
@allure.story("登录场景")
class TestLogin:

    @allure.title("正常登录用例")
    @pytest.mark.parametrize('username, password, resp_assert', get_login_good_case())
    def test_login_01(self,username, password, resp_assert):
        post_json = {"username": username, "password": password, "code": "",
                     "captchaKey": "d8db2330-beb9-11ec-b585-2f7541c6ef5e"}
        resp = requests.post(url, json=post_json)
        LOG.debug(resp.json())
        assert resp.json()["data"]["username"] == resp_assert

通过conftest.py提前写好登录接口,防止重复请求

@pytest.fixture(scope="class")
def test_login():
    resp = requests.post(login_url, json=post_json)
    print(resp.json())
    assert resp.json()["data"]["username"] == get_username()
    return resp.json()["data"]["token"]

调用conftest.py的方法,实现数据依赖

说明:调用test_login方法获取token

@allure.feature("学生端上课")
@allure.story("登录场景")
class TestCourseList:

    @allure.title("正常获取用户课程信息")
    @pytest.mark.parametrize('page,pagesize,className,courseName', get_good_case())
    def test_course_list_01(self, page, pagesize, className, courseName, test_login, test_user_info):
        headers = {"authorization": "Bearer " + test_login}  #获取token
        params = {"page": page, "pagesize": pagesize, "studentId": test_user_info}
        print("params==", params)
        allure.attach(url,"请求url",allure.attachment_type.TEXT)
        allure.attach(str(headers),"headers",allure.attachment_type.JSON)
        allure.attach(str(params),"params",allure.attachment_type.JSON)
        resp = requests.get(url, headers=headers, params=params)
        print(resp.json())
        allure.attach(str(resp.json()),"响应", allure.attachment_type.JSON)
        assert className in str(resp.json()["data"])
        assert courseName in str(resp.json()["data"])

使用allure生成测试报告

1、引入allure
2、使用allure相关方法设置title等

import allure

@allure.feature("学生端上课")
@allure.story("登录场景")
class TestLogin:

    @allure.title("正常登录用例")
    @pytest.mark.parametrize('username, password, resp_assert', get_login_good_case())
    def test_login_01(self,username, password, resp_assert):
        post_json = {"username": username, "password": password, "code": "",
                     "captchaKey": "d8db2330-beb9-11ec-b585-2f7541c6ef5e"}
        resp = requests.post(url, json=post_json)
        LOG.debug(resp.json())
        assert resp.json()["data"]["username"] == resp_assert

使用allure命令执行,并生成报告

if __name__ == '__main__':
    print_hi('PyCharm')
    # 执行pytest单元测试,生成 Allure 报告需要的数据存在 /report 目录
    pytest.main(['--alluredir', './report/result','test\\'])
    # 执行命令 allure generate ./report -o ./allure_html --clean ,生成测试报告
    os.system('allure generate ./report/result -o ./allure_html --clean')

命令:
1、执行pytest,生成报告数据:pytest -s --alluredir=./report/result
2、执行allure,生成报告页面:allure generate ./report/result -o ./allure_html --clean
在这里插入图片描述

定制化日志颜色

https://blog.csdn.net/beishida123/article/details/124616481?spm=1001.2014.3001.5502

定制化报告内容

说明:通过allure.attach方法实现定制化输出

@allure.feature("学生端上课")
@allure.story("登录场景")
class TestCourseList:

    @allure.title("正常获取用户课程信息")
    @pytest.mark.parametrize('page,pagesize,className,courseName', get_good_case())
    def test_course_list_01(self, page, pagesize, className, courseName, test_login, test_user_info):
        headers = {"authorization": "Bearer " + test_login}
        params = {"page": page, "pagesize": pagesize, "studentId": test_user_info}
        print("params==", params)
        allure.attach(url,"请求url",allure.attachment_type.TEXT)
        allure.attach(str(headers),"headers",allure.attachment_type.JSON)
        allure.attach(str(params),"params",allure.attachment_type.JSON)
        resp = requests.get(url, headers=headers, params=params)
        print(resp.json())
        allure.attach(str(resp.json()),"响应", allure.attachment_type.JSON)
        assert className in str(resp.json()["data"])
        assert courseName in str(resp.json()["data"])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值