"""
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中用例之前或之后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
"""
# setup_module 及 teardown_module模块,代码示例
import pytest

def setup_module():
print("setup_module:整个*.py只执行一次")
print('比如浏览器打开的动作')

def teardown_module():
print("teardown_module:整个*.py只执行一次")
print('比如浏览器关闭的动作')

def test_one():
print("正在执行------test_one")
AA = 'this'
assert 'i' in AA

def test_two():
print("正在执行------test_two")
BB = 'Str'
AA = 'Str'
assert AA == BB

def test_three():
print("正在执行------test_three")
AA = 'one'
assert 'i' != AA

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

运行代码结果:

# setup_function及tear_function 函数级模块,代码示例如下:

import pytest
def setup_function():
print("setup_function:每个用例开始前都会执行")

def teardown_function():
print("teardown_function:每个用例结束前都会执行")

def test_one():
print("正在执行------test_one")
AA = 'this'
assert 'i' in AA

def test_two():
print("正在执行------test_two")
BB = 'Str'
AA = 'Str'
assert AA == BB

def test_three():
print("正在执行------test_three")
AA = 'one'
assert 'i' != AA

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

# 运行结果如下:

优先级划分: setUp_class  》 setUp_module 》setUp

# 类和方法
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 'hello' == x

def test_three(self):
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b

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

代码执行结果如下:

先执行函数 --> 方法 --> 再执行class类里面的类方法

#函数和类混合
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 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

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

最新文章

  1. zookeeper集群
  2. mybatis多数据源配置
  3. osgconv 将多个模型合成一个模型
  4. jiulianhuan 快速幂--矩阵快速幂
  5. 2016年11月27日 星期日 --出埃及记 Exodus 20:18
  6. Zookeeper + Hadoop + Hbase部署备忘
  7. 负载均衡服务器session共享的解决方案
  8. android 应用静默自启动的解决方法
  9. OpenCV探索之路(三):滤波操作
  10. web页面font-family显示
  11. [Linux]Debian 9重启DNS重置问题
  12. 最大流:Dinic算法
  13. thinkphp 检测验证码
  14. vue滚动行为控制——页面跳转返回上一个页面保留滚动位置
  15. header头
  16. vscode 自定义快捷键
  17. 关于struts2中ActionContext类的作用
  18. nodejs 像 C 语言那样输出当前代码的行数
  19. Linq To Sql 使用初探
  20. 解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 问题(转载)

热门文章

  1. Spring Cloud专题之二:OpenFeign
  2. 使用ElementTree解析,操作xml
  3. 七、SSL加密网站(待解决)
  4. 关于Linux服务器部署
  5. Golang去除字符串前后空格
  6. IP地址与子网的划分
  7. redis字典快速映射+hash釜底抽薪+渐进式rehash | redis为什么那么快
  8. 源码搭建Zabbix4.0.23LTS监控系统
  9. .net获取项目根目录方法集合
  10. Docker:docker搭建redis一主多从集群(配置哨兵模式)