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

欢迎您给我留言,我们一起讨论。

你可能感兴趣的:(pytest测试框架系列 - setup和teardown的详细讲解)