liuyuelinfighting

1、setUp()和tearDown()函数介绍

之前学过Unittest测试框架,知道前置setup()函数和后置teardown()函数非常好用,在每次用例开始前和结束后都去执行一次。

当然还有更高级一点的setupClass()函数和teardownClass()函数,需配合classmethod装饰器一起使用,在做Selenium自动化的时候,它的效率尤为突出,可以只启动一次浏览器执行多个用例。

2、setUp()和tearDown()函数作用

  • setup()函数主要是进行测试前的初始化工作,比如:在接口测试前面做一些前置的参数赋值,数据库操作等等。
  • teardown()函数是测试后的清除工作,比如:参数还原或销毁,数据库的还原恢复等。

总结:setup()函数表示测试类中每个测试方法执行前都需要执行的操作,teardown()函数表示每个测试方法执行后都需要执行的操作。

3、setUp()和tearDown()函数说明

Pytest框架也有前置setup()函数和后置teardown()函数,并且还不止四个。

Pytest框架setUp()函数和tearDown()函数主要分为:模块级,类级,方法级,函数级。

说明每个级别的含义:

模块级:指的是一个.py文件。

类级:一个.py文件中可以写多个类。(一般情况下只写一个类)

方法级:类中定义的方法叫方法。

函数级:类外定义的方法叫函数。

Pytest框架提供的setUp()函数和tearDown()函数如下:

1)模块级与函数级,不定义在测试类中。

  • 模块级:setup_module()/teardown_module():开始于模块始末,全局的。
  • 函数级:setup_function()/teardown_function():只对函数用例生效(不在类中)。

2)类级与方法级,定义在类中。

  • 类级:setup_class()/teardown_class():只在类中前后运行一次(在类中)。
  • 方法级:setup_method()/teardown_method():开始于方法始末(在类中)。
  • *的:setup()/teardown():直接使用感觉和方法级前后置函数一样。

4、示例

(1)方法级

"""
setup_method()和 teardown_method()函数
需要定义在测试类中,定义在类外不起作用。
setup_method()定义场景,如:打开浏览器,加载网页等
teardown_method()场景,如:关闭浏览器等
"""
import pytest

# 测试类
class Test_setUp_tearDown:

    # 方法级,前置函数
    def setup_method(self):
        print("setup_method(self):在每个测试方法之前执行")


    # 方法级,后置函数
    def teardown_method(self):
        print("teardown_method(self):在每个测试方法之后执行\n")


    # 测试用例a
    def test_a(self):
        print("test_a方法")
        assert True

    # 测试用例b
    def test_b(self):
        print("test_b方法")
        assert True


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

"""
执行结果:

setup_method(self):在每个测试方法之前执行
test_a方法
teardown_method(self):在每个测试方法之后执行

PASSEDsetup_method(self):在每个测试方法之前执行
test_b方法
teardown_method(self):在每个测试方法之后执行
"""

(2)类级

"""
setup_class()和 teardown_class()函数
需要定义在测试类中,定义在类外不起作用。
setup_class()定义场景,比如:创建日志对象,创建数据库的连接,创建接口的请求对象等。
teardown_class()定义场景,比如:销毁日志对象,销毁数据库的连接,销毁接口的请求对象。
"""

import pytest


class Test_setUp_tearDown:

    # 方法级,前置函数
    def setup_method(self):
        print("setup_method(self):在每个测试方法之前执行")

    # 方法级,后置函数
    def teardown_method(self):
        print("teardown_method(self):在每个测试方法之后执行\n")

    # 类级,前置函数
    def setup_class(self):
        print("setup_class(self):每个测试类之前执行一次\n")

    # 类级,后置函数
    def teardown_class(self):
        print("teardown_class(self):每个测试类之后执行一次")

    # 测试用例a
    def test_a(self):
        print("test_a方法")
        assert True

    # 测试用例b
    def test_b(self):
        print("test_b方法")
        assert True


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

"""
执行结果:

setup_class(self):每个测试类之前执行一次

setup_method(self):在每个测试方法之前执行
test_a方法
teardown_method(self):在每个测试方法之后执行

PASSEDsetup_method(self):在每个测试方法之前执行
test_b方法
teardown_method(self):在每个测试方法之后执行

teardown_class(self):每个测试类之后执行一次
"""

(3)函数级

"""
setup_function()和 teardown_function()函数
需要定义在测试类外面,只负责函数的前后置。
对类中定义的方法,不起作用。
"""

import pytest

# 函数级,前置函数
def setup_function():
    print("setup_function: 每个函数开始前都会执行")

# 函数级,后置函数
def teardown_function():
    print("teardown_function: 每个函数结束都会执行\n")


# 测试用例a
def test_a():
    print("test_a函数")
    assert True


# 测试用例b
def test_b():
    print("test_b函数")
    assert True

# 测试类
class Test_setUp_tearDown:

    # 测试用例a
    def test_c(self):
        print("test_c方法")
        assert True

    # 测试用例b
    def test_d(self):
        print("test_d方法")
        assert True


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

"""
执行结果:
setup_function: 每个函数开始前都会执行
test_a函数
teardown_function: 每个函数结束都会执行

PASSEDsetup_function: 每个函数开始前都会执行
test_b函数
teardown_function: 每个函数结束都会执行

PASSEDtest_c方法
PASSEDtest_d方法
PASSED

"""

(4)模块级

"""
setup_module()和 teardown_module()函数
需要定义在测试类外面,
对函数和类中定义的方法,都不起作用。
"""

import pytest


# 模块级,前置函数
def setup_module():
    print("setup_module():在模块最之前执行\n")


# 模块级,后置函数
def teardown_module():
    print("teardown_module:在模块之后执行")


# 函数级,前置函数
def setup_function():
    print("setup_function: 每个函数开始前都会执行")


# 函数级,后置函数
def teardown_function():
    print("teardown_function: 每个函数结束都会执行\n")


# 测试用例a
def test_a():
    print("test_a函数")
    assert True


# 测试用例b
def test_b():
    print("test_b函数")
    assert True


# 测试类
class Test_setUp_tearDown:

    # 测试用例a
    def test_c(self):
        print("test_c方法")
        assert True

    # 测试用例b
    def test_d(self):
        print("test_d方法")
        assert True


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

"""
setup_module():在模块最之前执行

setup_function: 每个函数开始前都会执行
test_a函数
teardown_function: 每个函数结束都会执行

PASSEDsetup_function: 每个函数开始前都会执行
test_b函数
teardown_function: 每个函数结束都会执行

PASSEDtest_c方法
PASSEDtest_d方法
teardown_module:在模块之后执行
PASSED
"""

参考:https://blog.csdn.net/weixin_42550871/article/details/109107673

相关文章:

相关资源