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 小时前
自动化脚本开发:Python调用云手机API实现TikTok批量内容发布
android·网络安全·智能手机·架构·自动化
招风的黑耳4 小时前
Axure跨页面交互:利用IFrame和JS实现父子页面菜单联动
javascript·交互·axure
格发许可优化管理系统5 小时前
femap许可与云计算集成
大数据·网络·人工智能·自动化·云计算
筑梦之路5 小时前
centos 9 Kickstart + Ansible自动化部署 —— 筑梦之路
centos·自动化·ansible
明月与玄武18 小时前
SeleniumBase - 多合一浏览器自动化框架
自动化·seleniumbase
水银嘻嘻18 小时前
03 接口自动化-精通Postman之接口鉴权,接口Mock,接口加解密以及接口签名Sign
运维·自动化·postman
微刻时光19 小时前
DeepSeek赋能电商,智能客服机器人破解大型活动人力困境
人工智能·机器人·自动化·rpa·deepseek·影刀证书·影刀实战
Ro小陌1 天前
VisionPro自动化视觉开发实战:脚本编写、规范管理与高级调试
人工智能·microsoft·自动化
厦门辰迈智慧科技有限公司1 天前
灌区量测水自动化监测解决方案
运维·自动化
jwensh1 天前
【测试工具】selenium和playwright如何选择去构建自动化平台
selenium·测试工具·自动化