pytest笔记2: fixture

 1. fixture 通常是对测试方法和测试函数,测试类整个测试文件进行初始化或是还原测试环境

# 功能函数
def multiply(a, b):
    return a * b
# ------------ fixture---------------

def setup_module(module):
    print("======setup_module  在当前文件中所有测试用例之前=====")


def teardown_module(module):
    print("======teardown_module 在当前文件中所有测试用例之后=====")


def setup_function(function):
    print("setup_function=====>在每个测试函数之前执行")


def teardown_function(function):
    print("setup_function=====>在每个测试函数之后执行")


def setup():
    print("setup---在每个测试函数之前------>")


def teardown():
    print("teardown----在每个测试函数之后可以作用于类方法----->")


# ---------------测试用例-------------

def test_mult_3_4():
    print("test_mult_3_4")
    assert multiply(3, 4) == 12


def test_mult_a_4():
    print("test_mult_a_3")
    assert multiply('a', 3) == 'aaa'

 

没有 setup/teardown?

 

2.支持Test 开头的测试类

# 功能函数
def multiply(a, b):
    return a * b


class TestMultiply:
    # =====fixtures========
    @classmethod
    def setup_class(cls):
        print("setup_class=========>")

    @classmethod
    def teardown_class(cls):
        print("teardown_class=========>")

    def setup_method(self, method):
        print("setup_method----->>")

    def teardown_method(self, method):
        print("teardown_method-->>")

    def setup(self):
        print("setup----->")

    def teardown(self):
        print("teardown-->")

    # =====测试用例========
    def test_numbers_5_6(self):
        print('test_numbers_5_6')
        assert multiply(5, 6) == 30

    def test_strings_b_2(self):
        print('test_strings_b_2')
        assert multiply('b', 2) == 'bb'

 

rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\fixture
plugins: anyio-3.5.0
collected 2 items                                                                                                                                                    

test_fixtures_02.py setup_class=========>
setup_method----->>
test_numbers_5_6
.teardown_method-->>
setup_method----->>
test_strings_b_2
.teardown_method-->>
teardown_class=========>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值