Pytest测试用例之setup与teardown方法(二)

日常积累 | 初识Pytest  | 日常积累 | 初识pytest (二) | Pytest测试用例之setup与teardown方法(一)继续分享, 今天继模块级以及函数式setup与teardown之外的2种类与方法级的写法与执行顺序

回顾

pytest框架用例运行级别

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

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

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

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

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

接下来我们进入今天的小猪脚类与方法的setup、teardown

01类里面的

pytest 框架类里面的前置与后置用法setup、teardown ]

以下代码是类里面的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichao# python测试社区学习记录
import pytest

class Testcaselist():
    # 类里面的    def setup(self):        print('setup:每个用例前开始执行')
    def teardown(self):        print('teardown:每个用例后开始执行')
    # 测试用例    def test_001(self):        print("正在执行第一条用例")        p = "Python"        assert "h" in p
    def test_002(self):        print("正在执行第二条用例")        p = 'test'        assert 't' in p
    if __name__ == '__main__':        pytest.main(['-s', 'test_fixtclass.py'])



以下是代码执行后控制台输出

Testing started at 17:29 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 items
test_fixtclass.py::Testcaselist::test_001 setup:每个用例前开始执行PASSED                         [ 50%]正在执行第一条用例teardown:每个用例后开始执行
test_fixtclass.py::Testcaselist::test_002 setup:每个用例前开始执行PASSED                        [100%]正在执行第二条用例teardown:每个用例后开始执行

============================== 2 passed in 0.02s ==============================
Process finished with exit code 0

>>类里面的setup、teardown控制台输出解析

# 类里面的 [ 可以看到控制台输出的结果执行顺序 ]

test_fixtclass.py : test_001 >>setup:每个用例前开始执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown:每个用例后开始执行

test_fixtclass.py : test_002 >>setup:每个用例前开始执行>>PASSED>> [100%]正在执行第二条用例>>teardown:每个用例后开始执行

这是我们的类里面的setup、teardown作用对类里的测试用例生效

* 类里面的在每条测试用例执行前都会去执行一次

图例01

02类级

接着我们在看看[ 类级setup_class、teardown_class前置与后置用法 ]

以下代码是类级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8
# authou:shichao
# python测试社区学习记录
import pytest




class Testcaselist():
    print('setup_class:所有用例执行之前')


# 类级
def setup_class(self):
    print('setup_class:所有用例执行之前')


def teardown_class(self):
    print('teardown_class:所有用例执行结束之后')


# 测试用例
def test_001(self):
    print("正在执行第一条用例")
    p = "Python"
    assert "h" in p


def test_002(self):
    print("正在执行第二条用例")
    p = 'test'
    assert 't' in p
    
if __name__ == '__main__':
    pytest.main(['-s', 'test_fixtclass.py'])


以下是代码执行后控制台输出

Testing started at 20:12 ...
F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py
Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest


============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
collecting ... collected 2 items


test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前
PASSED                         [ 50%]正在执行第一条用例


test_fixtclass.py::Testcaselist::test_002 PASSED                         [100%]正在执行第二条用例
teardown_class:所有用例执行结束之后




============================== 2 passed in 0.02s ==============================


Process finished with exit code 0


>>类级setup_class、teardown_class控制台输出解析

# 类级 [ 我们可以看到控制台输出的结果执行顺序 ]

>>test_fixtclass.py:Testcaselist: >>setup_class:所有用例执行之前>>test_001 >>[ 50%]正在执行第一条用例>>PASSED  >>test_002>>[100%]正在执行第二条用例>>PASSED >>teardown_class:所有用例执行结束之后

* 类级前置后置只打开一次就执行所有的测试用例

图例02

03方法级

接着我们在看看[ 方法级setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichao# python测试社区学习记录
import pytest

class Testcaselist():
    # 方法级    def setup_method(self):        print('setup_method:每个用例开始之前执行')
    def teardown_method(self):        print('teardown_method:每个用例结束后执行')
    # 测试用例    def test_001(self):        print("正在执行第一条用例")        p = "Python"        assert "h" in p
    def test_002(self):        print("正在执行第二条用例")        p = 'test'        assert 't' in p
    if __name__ == '__main__':        pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 17:48 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 items
test_fixtclass.py::Testcaselist::test_001 setup_method:每个用例开始之前执行PASSED                         [ 50%]正在执行第一条用例teardown_method:每个用例结束后执行
test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行PASSED                         [100%]正在执行第二条用例teardown_method:每个用例结束后执行

============================== 2 passed in 0.02s ==============================
Process finished with exit code 0

>>方法级的setup_method、teardown_method控制台输出解析

# 类里面的 [ 可以看到控制台输出的结果执行顺序 ]

test_fixtclass.py : test_001 >>setup_method:每个用例开始之前执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown_method:每个用例结束后执行

test_fixtclass.py : test_002 >>setup_method:每个用例开始之前执行>>PASSED>> [100%]正在执行第二条用例>>teardown_method:每个用例结束后执行

* 方法级的在每条测试用例执行前都会去执行一次

图例03

04类级+方法级+类里面的

接着我们在看看[ 类级+方法级setup、teardown、setup_class、teardown_class、setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是类级+模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichao# python测试社区学习记录
import pytest

class Testcaselist():
    # 类里面的    def setup(self):        print('setup:每个用例前开始执行')
    def teardown(self):        print('teardown:每个用例后开始执行')
    # 类级    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 test_001(self):        print("正在执行第一条用例")        p = "Python"        assert "h" in p
    def test_002(self):        print("正在执行第二条用例")        p = 'test'        assert 't' in p
    if __name__ == '__main__':        pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 17:57 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 items
test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前setup_method:每个用例开始之前执行setup:每个用例前开始执行PASSED                         [ 50%]正在执行第一条用例teardown:每个用例后开始执行teardown_method:每个用例结束后执行
test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行setup:每个用例前开始执行PASSED                         [100%]正在执行第二条用例teardown:每个用例后开始执行teardown_method:每个用例结束后执行teardown_class:所有用例执行结束之后

============================== 2 passed in 0.02s ==============================
Process finished with exit code 0

>>类里面的+类级+方法级前置后置控制台输出解析

# 类里面的+类级+方法级 [ 我们可以看到控制台输出的结果执行顺序 ]

>>test_fixtclass.py::Testcaselist:setup_class:所有用例执行之前>>比如:所有用例开始前只打开一次浏览器

setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_001>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行

setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_002>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行

>>teardown_class:所有用例执行结束之后>>比如:所有用例结束只最后关闭浏览器

从结果看出,运行的优先级:setup_class>>setup_method>

setup >用例>teardown>teardown_method>>teardown_class

图例04

以上就是今天给大家介绍的pytest前置后置[ 类级以及方法级] 的用法以及在实际代码中他们的执行优先级,小小的顺序结构可能会影响你这条case是否执行通过,希望本次分享对大家有所帮助

注:本文内容来源于网络相关知识点综合总结,只作为知识分享,如有侵权可联系删除

日常积累,支持作者,可以将文章分享到朋友圈或点个在看,感谢大家的阅读!Bey , 下期再见

识别下方二维码,后台回复 "989" "Python" 领取 4G 入门到进阶成长资料

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
pytest和unittest都是Python中常用的测试框架。其中,setup和teardown是两个常用的测试用例执行前和执行后的钩子函数。 在pytest中,setup和teardown可以通过pytest.fixture装饰器来使用。装饰器可以被附加到函数、方法或类上,以标记其为fixture。当测试函数需要使用fixture时,它们可以将fixture名称作为输入参数,pytest将自动查找和运行fixture函数,并将其输出值传递给测试函数。例如: ```python import pytest @pytest.fixture def setup(): print("执行setup操作") yield print("执行teardown操作") def test_example(setup): print("执行测试操作") ``` 在这个例子中,setup函数被标记为fixture,test_example函数接收setup作为输入参数。在运行test_example函数之前,pytest将自动运行setup函数,然后运行测试函数,最后再运行teardown函数。 在unittest中,setup和teardown可以通过setUp和tearDown方法来实现。这些方法被定义在unittest.TestCase类中,并在每次运行测试方法之前和之后自动调用。例如: ```python import unittest class TestExample(unittest.TestCase): def setUp(self): print("执行setup操作") def tearDown(self): print("执行teardown操作") def test_example(self): print("执行测试操作") ``` 在这个例子中,TestExample类继承自unittest.TestCase类,并覆盖了setUp和tearDown方法。在运行test_example方法之前,unittest将自动调用setUp方法,然后运行测试方法,最后再调用tearDown方法。 总体而言,pytest和unittest都提供了简单易用的setup和teardown机制来帮助测试人员编写可靠的测试用例。但是,pytest相对于unittest更加灵活,可以通过fixture装饰器来定义setup和teardown函数,同时也提供了更多的扩展性和定制化选项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值