pytest(二)

学过unittest的都知道里面用前置和后置setupteardown非常好用,在每次用例开始前和结束后都去执行一次。当然还有更高级一点的setupClassteardownClass,需配合@classmethod装饰器一起使用

pytest中也有类似的语法:

模块级(setup_module/teardown_module)开始于模块始末,全局的

函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

类级(setup_class/teardown_class)只在类中前后运行一次(在类中)

方法级(setup_method/teardown_method)开始于方法始末(在类中)

类里面的(setup/teardown)运行在调用方法的前后

例1:

import pytest


def setup_module():
    print('所有用例执行前运行')

def teardown_module():
    print('所有用例执行后运行')

def setup_function():
    print('每条用例执行前都会运行')

def teardown_function():
    print('每条用例执行后都会运行')

def test_1():
    print('正在执行用例>1')

def test_2():
    print('正在执行用例>2')

if __name__ == '__main__':
    pytest.main('-q test_classs.py')
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: D:\PyCharmcode\untitled\pytestceshi, inifile:
plugins: metadata-1.7.0, html-1.19.0collected 2 items

test_classs.py 所有用例执行前运行
每条用例执行前都会运行
.正在执行用例>1
每条用例执行后都会运行
每条用例执行前都会运行
.正在执行用例>2
每条用例执行后都会运行
所有用例执行后运行
                                                        [100%]

========================== 2 passed in 0.05 seconds ===========================

从上述例子中我们可以看出:

setup_module是所有用例开始前只执行一次,teardown_module是所有用例结束后只执行一次

setup_function/teardown_function 每个用例开始和结束调用一次

例2:

import pytest



class Testclass:

    def setup_class(self):
        print('setup_class')

    def teardown_class(self):
        print('teardown_class')

    def setup_method(self):
        print('setup_method')

    def teardown_method(self):
        print('teardown_method')

    def setup(self):
        print('setup')

    def teardown(self):
        print('teardown')

    def test_1(self):
        print('用例 》 1')

    def test_2(self):
        print('用例 》 2')

if __name__ == '__main__':
    pytest.main('-q test_classs.py')
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: D:\PyCharmcode\untitled\pytestceshi, inifile:
plugins: metadata-1.7.0, html-1.19.0collected 2 items

test_classs.py setup_class
setup_method
setup
.用例 》 1
teardown
teardown_method
setup_method
setup
.用例 》 2
teardown
teardown_method
teardown_class
                                                        [100%]

========================== 2 passed in 0.05 seconds ===========================

从结果看出,运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

例3:

import pytest


def setup_module():
    print('所有用例执行前运行')

def teardown_module():
    print('所有用例执行后运行')

def setup_function():
    print('每条用例执行前都会运行')

def teardown_function():
    print('每条用例执行后都会运行')

def test_1():
    print('正在执行用例>1')

def test_2():
    print('正在执行用例>2')

class Testclass:

    def setup_class(self):
        print('setup_class')

    def teardown_class(self):
        print('teardown_class')

    def setup_method(self):
        print('setup_method')

    def teardown_method(self):
        print('teardown_method')

    def setup(self):
        print('setup')

    def teardown(self):
        print('teardown')

    def test_1(self):
        print('用例 》 1')

    def test_2(self):
        print('用例 》 2')

if __name__ == '__main__':
    pytest.main('-q test_classs.py')
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: D:\PyCharmcode\untitled\pytestceshi, inifile:
plugins: metadata-1.7.0, html-1.19.0collected 4 items

test_classs.py 所有用例执行前运行
每条用例执行前都会运行
.正在执行用例>1
每条用例执行后都会运行
每条用例执行前都会运行
.正在执行用例>2
每条用例执行后都会运行
setup_class
setup_method
setup
.用例 》 1
teardown
teardown_method
setup_method
setup
.用例 》 2
teardown
teardown_method
teardown_class
所有用例执行后运行
                                                      [100%]

========================== 4 passed in 0.07 seconds ===========================

从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值