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 开始于方法始末(在类中)
相关推荐
子午12 小时前
【蘑菇识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
人工智能·python·深度学习
Mr_Xuhhh12 小时前
pytest -- 指定⽤例执⾏顺序
开发语言·python·pytest
tokepson12 小时前
关于python更换永久镜像源
python·技术·记录
F_D_Z12 小时前
【解决办法】网络训练报错AttributeError: module ‘jax.core‘ has no attribute ‘Shape‘.
开发语言·python·jax
前端伪大叔12 小时前
第29篇:99% 的量化新手死在挂单上:Freqtrade 隐藏技能揭秘
后端·python·github
韩曙亮13 小时前
【人工智能】AI 人工智能 技术 学习路径分析 ① ( Python语言 -> 微积分 / 概率论 / 线性代数 -> 机器学习 )
人工智能·python·学习·数学·机器学习·ai·微积分
喵叔哟13 小时前
6.配置管理详解
后端·python·flask
曾经的三心草13 小时前
基于正倒排索引的Java文档搜索引擎3-实现Index类-实现搜索模块-实现DocSearcher类
java·python·搜索引擎
MOMO陌染14 小时前
Python 饼图入门:3 行代码展示数据占比
后端·python
vvoennvv15 小时前
【Python TensorFlow】 TCN-GRU时间序列卷积门控循环神经网络时序预测算法(附代码)
python·rnn·神经网络·机器学习·gru·tensorflow·tcn