pytest全局配置+前后只固件配置

pytest全局配置+前后只固件配置

通过读取pytest.ini配置文件运行

pytest.ini是pytest单元测试框架的核心配置文件

  1. 位置:一般放在项目的根目录
  2. 编码:必须是ANSI,可以使用notepad++修改编码格式
  3. 作用:改变pytest默认的行为
  4. 运行规则:不管是主函数的模式运行还是命令行模式运行,都会读取这个文件

通过读取pytest.ini配置文件运行

无条件跳过

pytest.ini

python 复制代码
[pytest]  ; pytest配置部分

addopts = -vs
;pytest的额外选项:
;-v: 详细模式,增加输出的详细程度
;-s: 不捕获stdout,允许显示print语句
;结合使用这些选项,可以提供详细的输出并允许在测试中显示print语句

testpaths=./testcases
; 指定测试用例所在的目录

python_files=test_*.py
; 测试文件的查找模式:
; 匹配以"ms_"开头并以".py"结尾的任何文件

python_classes=Test*
; 测试类的查找模式:
; 匹配以"Test"开头的任何类名

python_functions=test_*
; 测试函数的查找模式:
; 匹配以"test_"开头的任何函数名

#测试用例分组执行
markers =
        smoke:冒烟用例
        mashang:码尚教育
        product_manage:商品管理模块用例
        user_manage:用户管理模块

test_mashang.py

python 复制代码
from time import sleep




import pytest


class TestMashang:
    @pytest.mark.skip(reason="无理由跳过")
    # @pytest.mark.smoke
    def test_baili(self):
        sleep(3)
        print("测试百利老师")
        # raise Exception("百里老师开车")

    def test_tom(self):
        sleep(3)
        print("测试汤姆老师")

    # @pytest.mark.smoke
    def test_yiyao(self):
        sleep(3)
        print("测试一摇老师")


# 主函数
if __name__ == '__main__':
    pytest.main()

run.py

python 复制代码
import pytest
if __name__ == '__main__':
    pytest.main()

有条件跳过

test_mashang.py

python 复制代码
from time import sleep

import pytest


class TestMashang:
    workage = 8

    @pytest.mark.skip(reason="无理由跳过")
    # @pytest.mark.smoke
    def test_baili(self):
        # sleep(3)
        print("测试百利老师")
        # raise Exception("百里老师开车")

    def test_tom(self):
        # sleep(3)
        print("测试汤姆老师")

    @pytest.mark.skipif(workage < 10, reason="工龄小于10跳过")
    # @pytest.mark.smoke
    def test_yiyao(self):
        # sleep(3)
        print("测试一摇老师")


# 主函数
if __name__ == '__main__':
    pytest.main()

pytest框架实现的一些前后置(固件、夹具)处理

  • setup/teardown,setup_class/teardown_class
python 复制代码
    def setup_class(self):
        print("在每个类之前执行一次")
    def teardown_class(self):
        print("在每个类之后执行一次")


    def setup(self):
        print("在每个用例之前执行一次")
        # sleep(3)

    def teardown(self):
        print("在每个用例之后执行一次")

方法一(封装)

python 复制代码
class CommonUtil:
    def setup_class(self):
        print("在每个类之前执行一次")
    def teardown_class(self):
        print("在每个类之后执行一次")


    def setup(self):
        print("在每个用例之前执行一次")

    def teardown(self):
        print("在每个用例之后执行一次")

方法二(装饰器)

使用@pytest.fixture装饰器实现部分用例的前后置

@pytest.fixture装饰器的几个参数:

scope:表示被装饰器标记的方法的作用域

function:在函数之前和之后执行

class:在类之前和之后执行

package/session:在整个项目会话之前和之后执行

autouse:自动执行,默认是false

python 复制代码
@pytest.fixture(scope="function",autouse=True)
def exe_database_sql():
    print("执行sql查询")
    yield
    print("关闭数据库连接")

YAML文件实现接口自动化


python 复制代码
def read_yaml():
    return ['chenglong','zhenzidan','caiyilin']
@pytest.fixture(scope="function",autouse=False,params=read_yaml())
def exe_database_sql(request):
    print(request.param)
    print("执行sql查询")
    yield request.param
    print("关闭数据库连接")
相关推荐
科技快报1 小时前
引入实时 3D 渲染技术,地平线与 Unity 开启车载交互空间化时代
3d·unity·交互
程序员杰哥4 小时前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
_落纸6 小时前
《自动控制原理》第 3 章 线性控制系统的运动分析:3.6、3.7
笔记·自动化
蓝蜂物联网10 小时前
蓝蜂 MQTT 网关打通 120 台设备数据,助汽车零部件厂降本 40%
物联网·自动化·仪表
●VON14 小时前
初识影刀--一款 AI 驱动的 RPA 自动化软件
运维·自动化·rpa·影刀
学习3人组14 小时前
Python + requests + pytest + allure + Jenkins 构建完整的接口自动化测试框架
python·jenkins·pytest
一个处女座的程序猿O(∩_∩)O15 小时前
实现 AI 流式响应:从等待到实时交互的技术解析
网络·人工智能·交互
黄思搏18 小时前
Python + uiautomator2 手机自动化控制教程
python·智能手机·自动化
闲人编程1 天前
自动化文件管理:分类、重命名和备份
python·microsoft·分类·自动化·备份·重命名·自动化文件分类
ii_best1 天前
按键精灵安卓/iOS脚本辅助,OpenCV实现自动化高效率工具
ios·自动化·编辑器·安卓