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

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

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

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

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

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

3、Pytest框架的setUp()和tearDown()

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

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

说明每个级别的含义:

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

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

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

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

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

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

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

类级与方法级,定义在类中

类级: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

"""

 END绵薄之力

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

加入我的软件测试交流群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

获取方式 :

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: