1. 简介

学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次。
当然还有更高级一点的setupClass和teardownClass,需配合@classmethod装饰器一起使用,在做selenium自动化的时候,它的效率尤为突出,可以只启动一次浏览器执行多个用例。
pytest框架也有类似于setup和teardown的语法,并且还不止这四个

2. 用例运行级别

  • 模块级(setup_module/teardown_module)开始于模块始末,全局的

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

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

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

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

3. 函数式

3.1 setup_function/teardown_function (每个用例开始和结束时调用一次)

3.1.1 代码实现:

3.1.2 参考代码:
# coding=utf-8
# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行 # 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《带你装B,带你飞》pytest修仙之路3 - setup/teardown
'''
# 3.导入模块
# content of test_bjhg_class1.py
import pytest
# 函数式 def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束后都会执行") def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check') def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
3.1.3运行结果:

运行代码后,控制台打印如下图的结果

3.2 setup_module/teardown_module(所有用例开始和结束时调用一次)

3.2.1 代码实现:

3.2.2 参考代码:
# coding=utf-8
# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行 # 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《带你装B,带你飞》pytest修仙之路3 - setup/teardown
'''
# 3.导入模块
# content of test_bjhg_class1.py
import pytest
# 函数式 def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器") def teardown_module():
print("teardown_module:整个.py模块只执行一次")
print("比如:所有用例结束只最后关闭浏览器") def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束前都会执行") def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check') def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_bjhg_class1.py"])
3.2.3 运行结果:

运行代码后,控制台打印如下图的结果

3.3 类和方法

1.setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

3.3.1 代码实现:

3.3.2 参考代码:
# coding=utf-8
# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行 # 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《带你装B,带你飞》pytest修仙之路3 - setup/teardown
'''
# 3.导入模块
# content of test_bjhg_class1.py
import pytest
# 类和方法
class TestCase(): def setup(self):
print("setup: 每个用例开始前执行") def teardown(self):
print("teardown: 每个用例结束后执行") 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 test_one(self):
print("正在执行----test_one")
x = "this"
assert 'h' in x def test_two(self):
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check') def test_three(self):
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_bjhg_class1.py"])
3.3.3 运行结果:

运行代码后,控制台打印如下图的结果

备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可

3.4 函数和类混合

1.如果一个.py的文件里面既有函数用例又有类和方法用例,运行顺序又是怎样的呢?

3.4.1 代码实现:

3.4.2 参考代码:
# coding=utf-8
# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行 # 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《带你装B,带你飞》pytest修仙之路3 - setup/teardown
'''
# 3.导入模块
# content of test_bjhg_class1.py
import pytest
# 类和方法
def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器") def teardown_module():
print("teardown_module:整个.py模块只执行一次")
print("比如:所有用例结束只最后关闭浏览器") def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束前都会执行") def test_one():
print("正在执行----test_one")
x = "this"
assert 'h' in x def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check') class TestCase(): def setup_class(self):
print("setup_class:所有用例执行之前") def teardown_class(self):
print("teardown_class:所有用例执行之前") def test_three(self):
print("正在执行----test_three")
x = "this"
assert 'h' in x def test_four(self):
print("正在执行----test_four")
x = "hello"
assert hasattr(x, 'check') if __name__ == "__main__":
pytest.main(["-s", "test_bjhg_class1.py"])
3.4.3 运行结果:

运行代码后,控制台打印如下图的结果

2.从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

4. 小结

 好了,今天的分享就到这里吧!!!谢谢各位的耐心阅读。有问题加群交流讨论!!!

您的肯定就是我进步的动力。如果你感觉还不错,就请鼓励一下吧!记得随手点波  推荐  不要忘记哦!!!

别忘了点 推荐 留下您来过的痕迹

最新文章

  1. Javascript知识点记录(三)设计模式
  2. ssh访问控制,多次失败登录即封掉IP,防止暴力破解
  3. 使用jquery获取url及url参数的方法及定义JQuery扩展方法
  4. Java编程思想学习(二) 操作符
  5. startssl 申请证书,并在Nginx, apache, Tomcat上使用
  6. listview去掉条目间的分割线
  7. maven-编译速度优化
  8. DOS批处理中%cd%和%~dp0的区别[forward]
  9. JS的变量的值怎么传递给PHP的变量?
  10. 牛客网小a的旅行计划
  11. Windows 上安装 MySQL(8.0.11)
  12. Spring的后处理器-BeanPostProcessor跟BeanFactoryPostProcessors
  13. shell 中 标准输出和错误输出
  14. RecyclerView-- 侧滑删除和拖动排序
  15. Android-FragmentPagerAdapter刷新无效的解决方案
  16. 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
  17. mac/linux查询网络端口占用
  18. IE与DOM的事件监听
  19. synchronized和lock区别
  20. 百度网盘不能绑定QQ

热门文章

  1. ASCII代码表
  2. name和code的相关设置
  3. H3C 802.11无线网络的介质访问控制
  4. 基于thinkphp实现根据用户ip判断地理位置并提供对应天气信息的应用
  5. PHP redis安装扩展
  6. js利用select标签生成简易计算功能
  7. [转]Springboot和SpringMVC区别
  8. Codeforces Round #184 (Div. 2)
  9. 2019-6-5-WPF-隐藏系统窗口菜单
  10. 一目了然 | 数据库实例性能调优利器:Performance Insights