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