pytest_框架(3) - pytest固件、及用例执行顺序

pyest固件、及用例执行顺序

阅读目录:
  固件分类
  演示
  用例执行顺序

固件分类

概念: 固件用于执行前的初始化参数、执行后的清理动作

类型 规则
setup_module/ teardown_moduel全局模块级、运行模块前/后运行 (只运行一次)
setup_function/teardown_function函数级、 每个函数用例运行前/后运行
setup_class/teardown_clss类级、 每个class运行前/后运行(只运行一次)
setup_method(setup)/ teardown_method(teardown)方法级、 类中每个方法用例自行前/后运行 <== setup_method和setup、teardown_method和teardown二选一即可

演示

示例: 一个module, 两个函数, 两个类,每个类-两个方法
#! usr/bin/env python
# _*_ coding: utf-8 _*_
# @Author: zcs
# @wx: M_Haynes
# @Blog: https://editor.mdnice.com/?outId=3922c88879f84d9d87275683f6153499
# 备注: 主要用于介绍pytest固件、及用例执行顺序 -- 第三课


# 用例: 一个module, 两个函数, 两个类, 每个类两个方法

def setup_module():
    print("初始化: setup_module --- 全局模块级, 模块运行前 运行一次")

def teardown_module():
    print("清理:teardown_module --- 全局模块级, 模块运行后 运行一次")

def setup_function():
    print("初始化: setup_function -- 函数级, 每个函数用例运行前 运行一次")

def teardown_function():
    print("清理: teardown_function -- 函数级, 每个函数用例运行后  运行一次")

def test_example1():
    print(" ------------test_example1------------")
    assert 1==1

class TestExample3(object):
    def setup_clss(self):
        print("初始化:setup_class --- 类级, 每个class运行前   运行一次")

    def teardown_clss(self):
        print("清理: teardown_class --- 类级, 每个class运行后  运行一次")

    def test_example2(self):
        print(" ------------test_example2------------")
        assert 2==2

    def setup_function(self):
        print("初始化: setup_function")

    def teardown_function(self):
        print("清理: teardown_function")

    def test_example3(self):
        print(" ------------test_example3------------")


class TestExample4(object):
    def setup_class(self):
        print("初始化: setup_class2")

    def teardown_class(self):
        print("清理: teardown_class2")

    def setup_method(self):
        print("初始化: setup_method2")

    def teardown_method(self):
        print("清理: teardown_method2")

    def test_example4(self):
        print(" ------------test_example4------------")

    def test_example5(self):
        print(" ------------test_example5------------")

def test_example6():
    print(" ------------test_example6------------")

运行结果: image

用例执行顺序: 结论:pytest框架默认根据书写代码的先后顺序来执行 image

本文由 mdnice 多平台发布

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周承森

敲键盘不易~ 给点鼓励~ 感恩

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值