Pytest(一、基本使用)
1.Pytest介绍
2.Pytest–简单使用
3.始末方法(每次)
4.始末方法(一次)
5.执行顺序
6.pytest–跳过测试函数
7.pytest–函数数据参数化
1、Pytest介绍
1.pytest是python的一种单元测试框架,同自带的Unittest测试框架类似,相比于Unittest框架使用起来更简洁,效率更高.
2.支持参数化.
3.执行测试过程中可以将某些测试跳过,或者对某些预期失败的Case标记成失败
4.具有很多第三方插件,并且可以自定义扩展
安装命令 pip install pytest
2.Pytest--简单使用
# 脚本名: test_01_断言.py
import pytest
# 类名必须以 Test开头
class Test_study(object):
# 方法名也必须以 test开头
def test_a(self):
# 1 代表断言成功,不会报错
print('----使用1 不会报错----')
assert 1
def test_b(self):
# 0 代表断言失败,会报错
print('----使用0 会报错-----')
assert 0
if __name__ == '__main__':
# 需要传个列表 ["-s","脚本名称"]
# 直接就可以在pycharm中运行
pytest.main(["-s", "test_01_断言.py"])
"""
如果想在在命令行(终端)中执行
先来到该文件的目录下输入 : pytest 脚本名
eg: pytest test_01_断言.py
""
3.始末方法(每次)
# -*-coding:utf-8-*-
import pytest
"""
setup 方法:运行一次函数就被调用一次,且处于每次开头位置
teardown 方法: 运行一次函数就被调用一次,且处于每次结尾位置
"""
class Test_ABC:
def setup(self):
print("----每次开头被调用-----")
def teardown(self):
print("----每次结尾被调用----")
print()
def test_a(self):
print("----> test_a")
def test_b(self):
print("----> test_b")
if __name__ == "__main__":
pytest.main(["-s", "test_02_始末方法(每次).py"])
4.始末方法(一次)
# -*-coding:utf-8-*-
import pytest
class Test_study():
def setup_class(self):
print("---开头 只会执行一次----")
def teardown_class(self):
print("---结尾 只会执行一次----")
def test_a(self):
print("----> test_a")
def test_b(self):
print("----> test_b")
if __name__ == '__main__':
pytest.main(["-s", "test_03_始末方法(一次).py"])
5.执行顺序
# -*-coding:utf-8-*-
import pytest
"""
安装插件 :pip install pytest-ordering
@pytest.mark.run(order=x)
根据order传入参数来决定运行顺序:
order值全为正数或全为负数时, 值越小,优先级越高
正数和负数同时存在,正数优先级高
总结: 0 > 较小整数> 较大的整数 > 无标记 > 较小的负数 > 较大的负数
"""
class Test_demo():
def setup_class(self):
print("前置条件")
def teardown_class(self):
print("结尾")
@pytest.mark.run(order=2)
def test_b(self):
print(2)
@pytest.mark.run(order=1)
def test_c(self):
print(1)
@pytest.mark.run(order=3)
def test_a(self):
print(3)
if __name__ == '__main__':
pytest.main(["-s", "test_04_执行顺序.py"])
6.pytest--跳过测试函数
# 根据特定条件、不执行标识的测试函数
# @pytest.mark.skipif(condition, reason='xxx') 当condition为真时,不执行此函数
# -*-coding:utf-8-*-
import pytest
@pytest.mark.skipif(condition=1 > 1, reason="跳过")
def test_demo():
print("\n 条件为False时,此函数还是会被执行")
@pytest.mark.skipif(condition=2 > 1, reason="跳过")
def test_demo2():
print("\n 条件为True时,此函数不会被执行")
if __name__ == "__main__":
pytest.main(["-s", "test_01_pytest.py"])
# 运行结果:
test_01_pytest.py
条件为False时,此函数还是会被执行
.s
7.pytest--函数数据参数化
# 作用: 方便测试函数对测试属性的获取
"""
使用方法:
@pytest.mark.parametrize(argnames,argvalues)
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
常用参数:
argnames:参数名
argvalues:
- 参数对应值,类型必须为list
- 当参数为一个时,参数格式:[value]
- 当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)]
"""
import pytest
@pytest.mark.parametrize("num", [1, 2, 3, 4])
def test_demo(num):
print("当前值是:%s" % num)
if __name__ == "__main__":
pytest.main(["-s", "test_01_pytest.py"])
# 运行结果:
test_01_pytest.py 当前值是:1
.当前值是:2
.当前值是:3
.当前值是:4
.
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
文章标题:Pytest(一、基本使用)
本文作者:伟生
发布时间:2021-01-16, 15:19:03
最后更新:2021-01-23, 19:14:57
原始链接:http://yoursite.com/2021/01/16/ceshi_12_Pytest(1)/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。