脚本宝典收集整理的这篇文章主要介绍了pytest+allure基础知识,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
# 豆瓣源
pip install pytest -U -i http://pypi.douban.COM/simple/ --trusted-host pypi.douban.com
# 清华源
pip install pytest -U -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
# 用例匹配规则
[pytest]
python_files = test_*.py *_test.py
python_classes = Test*
python_functions = test_*
assert 1==2 #判断等式两边是否相等
assert 200 #判断某个语句是否为真
assert 10 in [10,20] #判断某个值是否属于某个对象
assert not True #判断某个语句是否不为真
assert 1!=2 #判断某个值是否不等于另一个值
def SETUP_module():
PRint('n *** 模块初始化开始 ***')
pass # 这里写初始化代码
print('n *** 模块初始化结束 ***')
def teardown_module():
print('n *** 模块清除开始 ***')
pass # 这里写清除代码
print('n *** 模块清除结束 ***')
class Test_A:
@classmethod
def setup_class(cls):
print(f'n === {cls.__name__}类初始化 ===')
@classmethod
def teardown_class(cls):
print(f'n === {cls.__name__}类清除 ===')
def setup_method(self):
print('n --- 类方法初始化 ---')
def teardown_method(self):
print('n --- 类方法清除 ---')
# 在类外面自定义一个方法,名字随意,但是要带上pytest的装饰器
@pytest.fixture(scoPE='function')
def custom_setup():
print(f'n --- 自定义方法初始化 ---')
pass # 在这写要初始化的代码
yield custom_setup # 初始化函数名(不要带括号)
custom_teardown() # 清除函数
# 同时要自定义一个清除函数,名字随意
def custom_teardown():
print(f'n --- 自定义方法清除 ---')
pass # 在这写清除的代码
# 如果有个方法需要调用自定义的初始化和清除函数,只需要把初始化函数传入即可
def test_abc(custom_setup):
pass
# 传入多个参数时,参数值列表中,每一组数据的类型都是元组
@pytest.mark.parametrize("val_a,val_b", [(1, 2), (3, 4), (5, 6)])
def test_parametrize_1(val_a,val_b):
print(f"变量val_a的值:{val_a},变量val_b的值:{val_b}")
# 可以使用多个参数化装饰器,测试数据为不同数据的笛卡尔积,实际会生成m*n条用例(下面的代码生成3*4=12条用例)
@pytest.mark.parametrize('a', [1,2,3])
@pytest.mark.parametrize('b', [4,5,6,7])
def test_parametrize_2(a, b):
print(f'笛卡尔积 测试数据为:{a},{b}')
# 在Pytst.ini文件的标记名列表中添加自定义的标记
[pytest]
markers =
smoke
test
hello
# 在需要标记的函数上增加装饰器(以smoke,hello为例)
@pytest.mark.smoke
def test_001(self):
print('我是一个冒烟测试用例')
@pytest.mark.hello
def test_002(self):
print('我是一个属于hello标记的测试用例')
# 执行所有标记为smoke的用例,显示详细过程,并打印print输出
pytest.main(['-k','smoke','-sv'])
# 导入子进程模块,避免运行时弹出警告
import subprocess
# 列表中第一个元素为pytest,后面的元素可以输入各种参数
subprocess.call(['pytest', '-k smoke', '-s'])
# 运行指定目录下用例
subprocess.call(['pytest', './test'])
# 运行指定模块
subprocess.call(['pytest', './test/test_demo.py'])
# 运行模块中的指定用例
subprocess.call(['pytest', './test/test_demo.py::test_001'])
# 运行类中的指定用例
subprocess.call(['pytest', './test/test_demo.py::TestDemo::test_001'])
# 运行test_demo.py模块下名称包含hello的用例
subprocess.call(['pytest', '-k hello','./test/test_demo.py'])
# 运行TestDemo类中名称包含aa的用例
subprocess.call(['pytest', '-k aa','./test/test_demo.py::TestDemo'])
pip install allure-pytest -i http://pypi.douban.com/simple/
@allure.epic('层级1')
@allure.feature('层级2')
@allure.story('层级3')
@allure.title('用例标题')
@allure.description('用例描述')
# 这里我是在每次执行的时候,将当天的测试数据和测试报告按日期+时间的双层目录来存放
date = time.strftime('%Y%m%d', time.localtime()) # 当前日期
time = time.strftime('%H%M%S', time.localtime()) # 当前时间
# allure命令要在命令行使用,在python中可通过os.System来调用
os.system(f'pytest --alluredir ./result/{date}/{time}/') # 生成测试结果
# 根据测试结果生成测试报告
# 加上--clean参数可以先清除数据,但由于我是按时间存放的,所以每次生成的目录都在不同位置,这里就不clean了,自己调试代码测试的时候可以用上clean
os.system(f'allure generate ./result/{date}/{time}/ -o ./report/{date}/{time}/')
# 自动打开测试报告,将启动后的服务ip换成 127.0.0.1
os.system(f'allure serve ./result/{date}/{time}/')
以上是脚本宝典为你收集整理的pytest+allure基础知识全部内容,希望文章能够帮你解决pytest+allure基础知识所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。