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=========>

猜你喜欢

转载自blog.csdn.net/oDianZi1234567/article/details/132679796