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 开始于方法始末(在类中)
相关推荐
途途途途1 小时前
Python 给 Excel 写入数据的四种方法
windows·python·excel
走在考研路上3 小时前
Python错误处理
开发语言·python
数据小爬虫@3 小时前
Python爬虫:如何优雅地“偷窥”商品详情
开发语言·爬虫·python
闰土_RUNTU3 小时前
Pytorch分布式训练print()使用技巧
人工智能·pytorch·python·分布式训练·训练技巧
零光速4 小时前
数据处理与统计分析——10-Pandas可视化-Matplotlib的常用API
数据结构·python·数据分析·pandas·matplotlib
雨中奔跑的小孩4 小时前
爬虫学习案例3
爬虫·python·学习
陌上笙清净4 小时前
flask内存马的真谛!!!
后端·python·网络安全·flask
A Genius5 小时前
Pytorch实现MobilenetV2官方源码
人工智能·pytorch·python
pzx_0016 小时前
【论文阅读】相似误差订正方法在风电短期风速预报中的应用研究
开发语言·论文阅读·python·算法·leetcode·sklearn
柠檬豆腐脑6 小时前
跨语言集成:将 Python 的强大功能带入 Nodejs 应用
前端·python·node.js