D91【python 接口自动化学习】- pytest基础用法

day91 pytest的setup,setdown详解(三)

学习日期:20241207

学习目标:pytest基础用法 -- pytest的setup,setdown详解(三)

学习笔记:

setup、teardown详解(三)
  • 类级 setup_class/teardown_class 只在类中前后运行一次(在类中)
python 复制代码
import requests
import pytest


class TestMobile:
    @classmethod
    def setup_class(cls):
        print("准备测试数据")

    @classmethod
    def teardown_class(cls):
        print("清理测试数据")

    def test_mobile(self):
        print("测试手机归属地get请求")
        r = requests.get('https://api.binstd.com/shouji/query',
                         params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"

    def test_mobile_post(self):
        print("测试手机归属地post请求")
        params = {
            "shouji": "13456755448",
            "appkey": "0c818521d38759e1"
        }
        r = requests.post('https://api.binstd.com/shouji/query', params=params)
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"


if __name__ == '__main__':
    pytest.main()
  • 方法级 setup_method/teardown_method 开始于方法始末(在类中)
python 复制代码
import requests
import pytest


class TestMobile:
    @classmethod
    def setup_method(cls):
        print("准备测试数据")

    @classmethod
    def teardown_method(cls):
        print("清理测试数据")

    def test_mobile(self):
        print("测试手机归属地get请求")
        r = requests.get('https://api.binstd.com/shouji/query',
                         params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"

    def test_mobile_post(self):
        print("测试手机归属地post请求")
        params = {
            "shouji": "13456755448",
            "appkey": "0c818521d38759e1"
        }
        r = requests.post('https://api.binstd.com/shouji/query', params=params)
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"


if __name__ == '__main__':
    pytest.main()
总结
  1. 类级 setup_class/teardown_class 只在类中前后运行一次(在类中)
  2. 方法级 setup_method/teardown_method 开始于方法始末(在类中)
相关推荐
AI效率工坊12 分钟前
【Python实战】数据可视化自动化:matplotlib+pyecharts+AI智能推荐图表类型
python·信息可视化·自动化
User_芊芊君子15 分钟前
2026 Python+AI入门|0基础速通,吃透热门轻量化玩法
开发语言·人工智能·python
好家伙VCC22 分钟前
**发散创新:基于Python的自动化恢复演练框架设计与实战**在现代软件系统运维中,
java·开发语言·python·自动化
爆更小哇26 分钟前
Python自动化测试:pytest新手快速入门指南
python·测试工具·自动化·pytest
西西弗Sisyphus29 分钟前
Python Lambda 表达式等价普通函数实现
python·lambda
张二娃同学35 分钟前
深度学习入门:YOLOv5 与 Fast R-CNN的认识
人工智能·python·深度学习·神经网络·yolo
海天一色y37 分钟前
深度学习时序预测进阶:CNN-LSTM-MHA混合模型+灰狼优化算法(GWO)实战
python
Yu_Lijing39 分钟前
Python数据分析和数据处理库Pandas(Series篇)
人工智能·python·数据分析·pandas
九河_1 小时前
从requirements.txt中安装缺失的包
python·conda·pip·环境管理
llm大模型算法工程师weng1 小时前
Python爬虫实现指南:从入门到实战
开发语言·爬虫·python