栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

简单了解pytest测试框架setup和tearDown

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单了解pytest测试框架setup和tearDown

pytest的setup与teardown

1)pytest提供了两套互相独立的setup 与 teardown和一对相对自由的setup与teardown

2)模块级与函数级

  模块级(setup_module/teardown_module)  #开始于模块始末(不在类中)

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

3)方法级与类级

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

  类级(setup_class/teardown_class)     #只在类中前后运行一次(在类中)

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

setup与teardown例子

import pytest
# 模块中的方法
def setup_module():
	print(
		"setup_module:整个test_module.py模块只执行一次"
	)
def teardown_module():
	print(
		"teardown_module:整个test_module.py模块只执行一次"
	)
def setup_function():
	print("setup_function:每个用例开始前都会执行")
def teardown_function():
	print("teardown_function:每个用例结束后都会执行")
# 测试模块中的用例1
def test_one():
	print("正在执行测试模块----test_one")
# 测试模块中的用例2
def test_two():
	print("正在执行测试模块----test_two")
# 测试类
class TestCase():
	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_three(self):
	print("正在执行测试类----test_three")
def test_four(self):
	print("正在执行测试类----test_four")
if __name__ == "__main__":
	pytest.main(["-s", "test_module.py"])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/19972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号