pytest测试框架系列 - setup和teardown的详细讲解

简介

      前面使用过unittest的都知道,setup和teardown是用来处理用例的前置工作和清理工作,其中还有setupclass和teardownclass是保证执行所以的用例都只执行1次前置和后置,使用起来非常方便,对于强大的pytest测试框框,给我们提供了10中类似方法。

  • 模块级别:setup_module、teardown_module
  • 函数级别:setup_function、teardown_function
  • 类级别:setup_class、teardown_class
  • 类方法级别:setup_method、teardown_method
  • 函数和方法级别:setup、teardown

详细用法讲解

模块级别:setup_module、teardown_module

说明:该方法表示只能类外面执行用例过程中,只执行1次。相当于unittest中的setupclass和teardownclass方法

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】

import pytest

def setup_module():
    print('这是setup_module的前置')

def teardown_module():
    print('这是setup_module的后置')

def test_01():
    print('我是test_01用例')

class TestDemo:
    def test_one(self):
        print("我是类里面方法 test_one")
    def test_two(self):
        print("我是类里面方法 test_two")
        
if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
setup_module和setup_module

函数级别:setup_function、teardown_function

说明:该方法表示在类外面执行用例过程中,每次都会执行前置和后置。

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest

def setup_function():
    print('这是 setup_function 的前置')

def teardown_function():
    print('这是 teardown_function 的后置')

def test_01():
    print('我是类外面的 test_01 用例')

class TestDemo:
    def test_one(self):
        print("我是类里面方法 test_one")
    def test_two(self):
        print("我是类里面方法 test_two")

if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
setup_function和teardown_function

类级别:setup_class、teardown_class

说明:该方法表示在类中执行测试用例前,只执行1次测试前置和测试后置,注意:放在类外面不生效

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest

def test_01():
    print('我是类外面的 test_01 用例')

class TestDemo:
    def setup_class(self):
        print("我是 setup_class 方法")

    def teardown_class(self):
        print("我是 teardown_class 方法")

    def test_one(self):
        print("我是类里面方法 test_one")

    def test_two(self):
        print("我是类里面方法 test_two")

if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
setup_class和teardown_class

方法级别:setup_method、teardown_method

说明:该方法表示在类中每次执行测试用例前,测试前置和测试后置都会执行一次,注意:放在类外面不生效

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest

def test_01():
    print('我是类外面的 test_01 用例')

class TestDemo:
    def setup_method(self):
        print("我是 setup_method 方法")
        
    def teardown_method(self):
        print("我是 teardown_method 方法")

    def test_one(self):
        print("我是类里面方法 test_one")

    def test_two(self):
        print("我是类里面方法 test_two")

if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
setup_method和teardown_method

函数和方法级别:setup、teardown

说明:该方法这个可以在类中使用,也可以在类外进行使用,大家最熟悉

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest

def setup():
    print("我是类外面 setup 方法")

def teardown():
    print("我是类外面 teardown 方法")

def test_01():
    print('我是类外面的 test_01 用例')

class TestDemo:
    def setup(self):
        print("我是类里面 setup 方法")

    def teardown(self):
        print("我是类里面 teardown 方法")

    def test_one(self):
        print("我是类里面方法 test_one")

    def test_two(self):
        print("我是类里面方法 test_two")

if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
setup和teardown

多个方法组合使用

说明:直接查看示例,查看各个方法执行优先级,非常重要

示例如下:

# _*_coding:utf-8 _*_

# @Time  :2021/7/2 22:18
# @Author  : king
# @File    :test_demo01.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest

def setup_module():
    print("我是类外面的 setup_module 方法")

def teardown_module():
    print("我是类外面的 teardown_module 方法")

def setup_function():
    print("我是类外面的 setup_function 方法")

def teardown_function():
    print("我是类外面的 teardown_function 方法")

def setup():
    print("我是类外面 setup 方法")

def teardown():
    print("我是类外面 teardown 方法")

def test_one():
    print("我是类外面的 test_one 用例")

def test_two():
    print("我是类外面的 test_two 用例")

class TestDemo():
    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_one(self):
        print("我是类里面的 test_one 用例")

    def test_two(self):
        print("我是类里面的 test_two 用例")

if __name__ == '__main__':
    pytest.main()

命令行输入: pytest -s test_demo01.py 执行结果如下:
组合使用

我们根据执行结果看见方法执行顺序

  • setup_module、teardown_module 整个测试只执行一次
  • setup_function、teardown_function 类外面方法每个用例都执行
  • setup_class、teardown_class 类里面只执行一次
  • setup_method、teardown_method 类里面每个用例都执行
  • setup、teardown 类里面和外面每个用例都执行

优先级为:

  • setup_module>setup_function>setup>用例>teardown>teardown_function>teardown_module
  • setup_module>setup_class>setup_method>setup>用例>teardown>teardown_method>teardown_class>teardown_module

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章
个人微信号

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试之路king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值