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("关闭数据库连接")
相关推荐
Wpa.wk4 小时前
自动化测试环境配置-java+python
java·开发语言·python·测试工具·自动化
William_cl4 小时前
【ASP.NET Core】Controller 层 Action 返回值精讲:JsonResult(AJAX 交互核心)
ajax·asp.net·交互
西游音月14 小时前
(4)pytest+Selenium自动化测试-元素定位之CSS Selector定位
css·selenium·pytest
0思必得018 小时前
[Web自动化] HTTP/HTTPS协议
前端·python·http·自动化·网络基础·web自动化
fruge1 天前
前端自动化脚本:用 Node.js 写批量处理工具(图片压缩、文件重命名)
前端·node.js·自动化
啃火龙果的兔子1 天前
webview焦点与软键盘键盘交互详解
计算机外设·交互
吴法刚1 天前
Gemini cli 源码分析之-Gemini CLI 项目启动交互模式startInteractiveUI函数
ai·交互·ai编程·gemini·ai编码
代码对我眨眼睛1 天前
Ubuntu 系统 NVIDIA 显卡驱动自动化安装全流程
linux·ubuntu·自动化
施努卡机器视觉2 天前
SNK施努卡车门自动化安装
运维·自动化
2401_77CN2 天前
汇川Easy系列PLC封装一阶低通滤波器FC【双线性变换】(定时中断应用,固定步长求解)
笔记·自动化