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("关闭数据库连接")
相关推荐
cape_NO_711 分钟前
运动控制卡--概述学习
学习·自动化
互联网杂货铺3 小时前
python+pytest 接口自动化测试:参数关联
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
囚~徒~8 小时前
flask 接口文档自动化
python·flask·自动化
SuperCreators1 天前
DeepSeek与浏览器自动化AI Agent构建指南
人工智能·自动化
挽风8211 天前
Pytest自动化框架
自动化·pytest·模块测试
qq_白羊座1 天前
UI自动化:Python + Selenium4.6+版本 环境搭建
python·自动化
Python数据分析与机器学习1 天前
《基于锂离子电池放电时间常数的自动化电量评估系统设计》k开题报告
运维·性能优化·自动化·软件工程·软件构建·个人开发
_深海凉_1 天前
OmniParser技术分析(一)
深度学习·目标检测·ui·自动化
学金融的程序员懒羊羊1 天前
散户如何实现自动化交易下单——篇1:体系介绍与获取同花顺资金账户和持仓信息
运维·自动化
微丽宝1 天前
OWL-开源多智能体协作系统,动态交互实现任务自动化
运维·自动化