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("关闭数据库连接")
相关推荐
2301_787552873 小时前
AI-Browser适用于 ChatGPT、Gemini、Claude、DeepSeek、Grok的客户端开源应用程序,集成了 Monaco 编辑器。
人工智能·chatgpt·自动化·编辑器·deepseek
struggle202510 小时前
deepseek-cli开源的强大命令行界面,用于与 DeepSeek 的 AI 模型进行交互
人工智能·开源·自动化·交互·deepseek
fish_study_csdn11 小时前
pytest 技术总结
开发语言·python·pytest
leo·Thomas12 小时前
Rundeck 介绍及安装:自动化调度与执行工具
docker·自动化·运维工具·rundeck
若愚679219 小时前
前端与Rust后端交互:跨越语言鸿沟 (入门系列三)
前端·rust·交互
若愚679220 小时前
Vue3 + OpenLayers 开发教程 ( 三 ) 交互与空间分析
arcgis·vue·交互
PM大明同学1 天前
Axure PR 9 中继器 标签
ui·交互·产品经理·axure
Ctrl С1 天前
[三分钟]入门web自动化测试(一):1.使用驱动管理下载web驱动(WebDriver);2.初步认识和使用Selenium
selenium·测试工具·自动化·web
qq_400552001 天前
CI/CD自动化部署(持续集成和持续交付/部署)
运维·ci/cd·自动化
难以怀瑾1 天前
pytest心得体会
pytest