自动化框架之setup,teardown和pytest的fixture装饰器

1.初始化,清除(setup,teardown)

自动化测试框架来说,初始化,清除功能非常重要!!!

1.模块级别的初始化和清除

该功能的作用:主要是用来为该模块中所有的测试用例做公共的初始化和清除。初始化、清除分别在整个模块的测试用例执行前后执行,并且只会执行1次

def setup_module():
    print('\n *** start ***')

def teardown_module():
    print('\n *** end***')

class Test_assert:

    def test_id1(self):
        print('\ncase1')
        assert 1 == 1

class Test_assert2:

    def test_id2(self):
        print('\ncase2')
        assert 2 == 2
 *** start ***
test_5.py::Test_assert::test_id1 
hello
PASSED                                                                   [50%]                                           
case1

test_5.py::Test_assert2::test_id2 PASSED                                 [100%]
case2

 *** end***

从运行结果可以看到:以上的打印start为初始化功能,打印end为清除功能。初始化、清除分别在整个模块的测试用例执行前后执行,并且只会执行1次 

ps:在工作中一般登陆某个页面会作为初始化,登出和关闭页面会作为清除功能。

1.2类级别的初始化,清除

类级别的初始化、清除分别在整个的测试用例执行前后执行,并且只会执行1次

def setup_module():
    print('\n *** start ***')

def teardown_module():
    print('\n *** end***')

class Test_assert:

    def setup_class(cls):
        print('\n ***start-class***')

    def teardown_class(cls):
        print('\n ***end-class***')

    def test_id1(self):
        print('\ncase1')
        assert 1 == 1

class Test_assert2:

    def test_id2(self):
        print('\ncase2')
        assert 2 == 2
test_6.py::Test_assert::test_id1 
 *** start ***

 ***start-class***
PASSED                                  [ 50%]
case1

 ***end-class***

test_6.py::Test_assert2::test_id2 PASSED                                 [100%]
case2

 *** end***

1.3方法级别的初始化,清除

方法级别的初始化、清除分别在类的每个测试方法执行前后执行,并且 每个用例分别执行1次


def setup_module():
    print('\n *** start ***')

def teardown_module():
    print('\n *** end***')

class Test_assert:

    def setup_class(cls):
        print('\n ***start-class***')

    def teardown_class(cls):
        print('\n ***end-class***')

    def setup_method(self):
        print('\n ***start-method***')

    def teardown_method(self):
        print('\n ***end-method***')

    def test_id1(self):
        print('\ncase1')
        assert 1 == 1

class Test_assert2:

    def test_id2(self):
        print('\ncase2')
        assert 2 == 2
test_7.py::Test_assert::test_id1 
 *** start ***

 ***start-class***

 ***start-method***
PASSED                                  [ 50%]
case1

 ***end-method***

 ***end-class***

test_7.py::Test_assert2::test_id2 PASSED                                 [100%]
case2

 *** end***

从结果可看出,方法级别的初始化、清除在整个模块所有用例执行前后分别执行一次

1.4目录级别

目标级别的初始化清除,就是针对整个目录执行的初始化、清除。

一般在工作中我们会在需要初始化的目录下面创建 一个名为 conftest.py 的文件。该文件的内容就是做目标级别的初始化清除。

conftest代码如下:

import pytest

@pytest.fixture(scope='package', autouse=True)
def st_emptyEnv():
    print('\nstart package')
    yield
    print('\nend package')

这样就会有目标级别的初始化清除,把test5运行一下查看效果

test_5.py::Test_assert::test_id1 
start package

 *** start ***
PASSED                                  [ 50%]
case1

test_5.py::Test_assert2::test_id2 PASSED                                 [100%]
case2

 *** end***

end package

2.fixture装饰器

(setup/teardown)是unnitest的传统单元测试,pytest的fixture有显著改进。与setup和teardown相比,fixture使用起来更加灵活。

fixture的作用是:能够实现前置动作(相当于Setup)

使用方法:

pytest.fixture(scope='function', params=None, autouse=False, ids=None, name=None)

常用参数:
- scope: 被标记方法的作用域
        "function"(测试函数级别): 默认值,表示每个测试方法都要执行一次
        "class"(测试类级别): 作用于整个类, 表示每个类的所有测试方法只运行一次
        "module"(测试模块级别): 作用于整个模块, 每个module的所有测试方法只运行一次.
        "session"(多个文件级别): 作用于整个session, 每次session只运行一次. (慎用!!)

- params: list类型,默认None, 接收参数值,对于param里面的每个值,fixture都会去遍历执行一次.
- autouse: 是否自动运行,默认为false, 为true时此session中的所有测试函数都会调用fixture

举个栗子:

指定一个类test_id1方法之前运行一个sayhello

@pytest.fixture(scope="class")

import pytest

@pytest.fixture(scope="class")
def sayhello():
    print("hello")

class Test_assert:
    #在ID1这个方法中调用了sayhello,故运行时sayhello方法会运行在test_id1之前
    def test_id1(self,sayhello):
        print('\ncase1')
        assert 1 == 1

class Test_assert2:
    #在id2这个方法中未调用sayhello,故不会运行sayhello方法
    def test_id2(self):
        print('\ncase2')
        assert 2 == 2
test_5.py::Test_assert::test_id1 

hello

PASSED                                                                  [ 50%]
case1

test_5.py::Test_assert2::test_id2 PASSED                                 [100%]
case2

其他情况就不再举例。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest.fixture装饰pytest测试框架中的一个重要特性,用于定义测试用例中的共享资源或者测试环境的初始化和清理操作。通过使用fixture装饰,我们可以在测试用例中方便地使用这些共享资源。 fixture装饰可以应用在函数、类或者模块级别上,它的作用是将被装饰的函数或者方法转变为一个fixture对象。fixture对象可以在测试用例中作为参数进行调用,pytest会自动根据参数名匹配相应的fixture对象,并将其传递给测试用例。 fixture装饰可以接受一些参数来定制其行为,例如scope参数用于指定fixture的作用域,autouse参数用于指定是否自动使用fixture等。 下面是一些常见的fixture用法: 1. 无参数fixture: ```python import pytest @pytest.fixture def setup(): # 初始化操作 yield # 清理操作 def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 2. 带参数fixture: ```python import pytest @pytest.fixture def setup(request): # 初始化操作 def teardown(): # 清理操作 request.addfinalizer(teardown) def test_example(setup): # 使用setup fixture assert 1 + 1 == 2 ``` 3. fixture作用域: ```python import pytest @pytest.fixture(scope="module") def setup_module(): # 模块级别的初始化操作 yield # 模块级别的清理操作 @pytest.fixture(scope="function") def setup_function(): # 函数级别的初始化操作 yield # 函数级别的清理操作 def test_example(setup_module, setup_function): # 使用setup_module和setup_function fixture assert 1 + 1 == 2 ``` 通过使用fixture装饰,我们可以更加灵活地管理测试用例中的共享资源和测试环境的初始化和清理操作,提高测试用例的可维护性和可重复性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值