• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest测试用例:setup和teardown方法(二)

武飞扬头像
测试店小二
帮助1

回顾

pytest框架用例运行级别

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

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

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

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

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

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

一、类里面的

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

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

  1.  
    # coding=utf-8
  2.  
    # authou:shichao
  3.  
     
  4.  
    import pytest
  5.  
     
  6.  
     
  7.  
    class Testcaselist():
  8.  
     
  9.  
    # 类里面的
  10.  
    def setup(self):
  11.  
    print('setup:每个用例前开始执行')
  12.  
     
  13.  
    def teardown(self):
  14.  
    print('teardown:每个用例后开始执行')
  15.  
     
  16.  
    # 测试用例
  17.  
    def test_001(self):
  18.  
    print("正在执行第一条用例")
  19.  
    p = "Python"
  20.  
    assert "h" in p
  21.  
     
  22.  
    def test_002(self):
  23.  
    print("正在执行第二条用例")
  24.  
    p = 'test'
  25.  
    assert 't' in p
  26.  
     
  27.  
    if __name__ == '__main__':
  28.  
    pytest.main(['-s', 'test_fixtclass.py'])
学新通

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

  1.  
    Testing started at 17:29 ...
  2.  
    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
  3.  
    Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
  4.  
     
  5.  
    ============================= test session starts =============================
  6.  
    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
  7.  
    cachedir: .pytest_cache
  8.  
    rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
  9.  
    collecting ... collected 2 items
  10.  
     
  11.  
    test_fixtclass.py::Testcaselist::test_001 setup:每个用例前开始执行
  12.  
    PASSED [ 50%]正在执行第一条用例
  13.  
    teardown:每个用例后开始执行
  14.  
     
  15.  
    test_fixtclass.py::Testcaselist::test_002 setup:每个用例前开始执行
  16.  
    PASSED [100%]正在执行第二条用例
  17.  
    teardown:每个用例后开始执行
  18.  
     
  19.  
     
  20.  
    ============================== 2 passed in 0.02s ==============================
  21.  
     
  22.  
    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作用对类里的测试用例生效

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

学新通

二、类级

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

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

  1.  
    # coding=utf-8
  2.  
    # authou:shichao
  3.  
    import pytest
  4.  
     
  5.  
     
  6.  
    class Testcaselist():
  7.  
    print('setup_class:所有用例执行之前')
  8.  
     
  9.  
    # 类级
  10.  
    def setup_class(self):
  11.  
    print('setup_class:所有用例执行之前')
  12.  
     
  13.  
    def teardown_class(self):
  14.  
    print('teardown_class:所有用例执行结束之后')
  15.  
     
  16.  
    # 测试用例
  17.  
    def test_001(self):
  18.  
    print("正在执行第一条用例")
  19.  
    p = "Python"
  20.  
    assert "h" in p
  21.  
     
  22.  
    def test_002(self):
  23.  
    print("正在执行第二条用例")
  24.  
    p = 'test'
  25.  
    assert 't' in p
  26.  
     
  27.  
    if __name__ == '__main__':
  28.  
    pytest.main(['-s', 'test_fixtclass.py'])
学新通

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

  1.  
    Testing started at 20:12 ...
  2.  
    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
  3.  
    Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
  4.  
     
  5.  
    ============================= test session starts =============================
  6.  
    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
  7.  
    cachedir: .pytest_cache
  8.  
    rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
  9.  
    collecting ... collected 2 items
  10.  
     
  11.  
    test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前
  12.  
    PASSED [ 50%]正在执行第一条用例
  13.  
     
  14.  
    test_fixtclass.py::Testcaselist::test_002 PASSED [100%]正在执行第二条用例
  15.  
    teardown_class:所有用例执行结束之后
  16.  
     
  17.  
     
  18.  
    ============================== 2 passed in 0.02s ==============================
  19.  
     
  20.  
    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:所有用例执行结束之后

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

 学新通

三、方法级

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

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

  1.  
    # coding=utf-8
  2.  
    # authou:shichao
  3.  
     
  4.  
    import pytest
  5.  
     
  6.  
     
  7.  
    class Testcaselist():
  8.  
     
  9.  
    # 方法级
  10.  
    def setup_method(self):
  11.  
    print('setup_method:每个用例开始之前执行')
  12.  
     
  13.  
    def teardown_method(self):
  14.  
    print('teardown_method:每个用例结束后执行')
  15.  
     
  16.  
    # 测试用例
  17.  
    def test_001(self):
  18.  
    print("正在执行第一条用例")
  19.  
    p = "Python"
  20.  
    assert "h" in p
  21.  
     
  22.  
    def test_002(self):
  23.  
    print("正在执行第二条用例")
  24.  
    p = 'test'
  25.  
    assert 't' in p
  26.  
     
  27.  
    if __name__ == '__main__':
  28.  
    pytest.main(['-s', 'test_fixtclass.py'])
学新通

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

  1.  
    Testing started at 17:48 ...
  2.  
    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
  3.  
    Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
  4.  
     
  5.  
    ============================= test session starts =============================
  6.  
    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
  7.  
    cachedir: .pytest_cache
  8.  
    rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
  9.  
    collecting ... collected 2 items
  10.  
     
  11.  
    test_fixtclass.py::Testcaselist::test_001 setup_method:每个用例开始之前执行
  12.  
    PASSED [ 50%]正在执行第一条用例
  13.  
    teardown_method:每个用例结束后执行
  14.  
     
  15.  
    test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行
  16.  
    PASSED [100%]正在执行第二条用例
  17.  
    teardown_method:每个用例结束后执行
  18.  
     
  19.  
     
  20.  
    ============================== 2 passed in 0.02s ==============================
  21.  
     
  22.  
    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:每个用例结束后执行

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

 学新通

四、类级 方法级 类里面的

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

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

  1.  
    # coding=utf-8
  2.  
    # authou:shichao
  3.  
     
  4.  
    import pytest
  5.  
     
  6.  
     
  7.  
    class Testcaselist():
  8.  
     
  9.  
    # 类里面的
  10.  
    def setup(self):
  11.  
    print('setup:每个用例前开始执行')
  12.  
     
  13.  
    def teardown(self):
  14.  
    print('teardown:每个用例后开始执行')
  15.  
     
  16.  
    # 类级
  17.  
    def setup_class(self):
  18.  
    print('setup_class:所有用例执行之前')
  19.  
     
  20.  
    def teardown_class(self):
  21.  
    print('teardown_class:所有用例执行结束之后')
  22.  
     
  23.  
    # 方法级
  24.  
    def setup_method(self):
  25.  
    print('setup_method:每个用例开始之前执行')
  26.  
     
  27.  
    def teardown_method(self):
  28.  
    print('teardown_method:每个用例结束后执行')
  29.  
     
  30.  
    # 测试用例
  31.  
    def test_001(self):
  32.  
    print("正在执行第一条用例")
  33.  
    p = "Python"
  34.  
    assert "h" in p
  35.  
     
  36.  
    def test_002(self):
  37.  
    print("正在执行第二条用例")
  38.  
    p = 'test'
  39.  
    assert 't' in p
  40.  
     
  41.  
    if __name__ == '__main__':
  42.  
    pytest.main(['-s', 'test_fixtclass.py'])
学新通

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

  1.  
    Testing started at 17:57 ...
  2.  
    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
  3.  
    Launching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest
  4.  
     
  5.  
    ============================= test session starts =============================
  6.  
    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
  7.  
    cachedir: .pytest_cache
  8.  
    rootdir: F:\python3\python_code\Study\API_AutoTest_Pytest
  9.  
    collecting ... collected 2 items
  10.  
     
  11.  
    test_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前
  12.  
    setup_method:每个用例开始之前执行
  13.  
    setup:每个用例前开始执行
  14.  
    PASSED [ 50%]正在执行第一条用例
  15.  
    teardown:每个用例后开始执行
  16.  
    teardown_method:每个用例结束后执行
  17.  
     
  18.  
    test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行
  19.  
    setup:每个用例前开始执行
  20.  
    PASSED [100%]正在执行第二条用例
  21.  
    teardown:每个用例后开始执行
  22.  
    teardown_method:每个用例结束后执行
  23.  
    teardown_class:所有用例执行结束之后
  24.  
     
  25.  
     
  26.  
    ============================== 2 passed in 0.02s ==============================
  27.  
     
  28.  
    Process finished with exit code 0
学新通
  1.  
    >>类里面的 类级 方法级前置后置控制台输出解析
  2.  
     
  3.  
    # 类里面的 类级 方法级 [ 我们可以看到控制台输出的结果执行顺序 ]
  4.  
     
  5.  
    >>test_fixtclass.py::Testcaselist:setup_class:所有用例执行之前>>比如:所有用例开始前只打开一次浏览器
  6.  
     
  7.  
    setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_001>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行
  8.  
     
  9.  
    setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_002>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行
  10.  
     
  11.  
    >>teardown_class:所有用例执行结束之后>>比如:所有用例结束只最后关闭浏览器
  12.  
     
  13.  
    从结果看出,运行的优先级:setup_class>>setup_method>
  14.  
     
  15.  
    setup >用例>teardown>teardown_method>>teardown_class
学新通

 学新通

B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikebe
系列文章
更多 icon
同类精品
更多 icon
继续加载