场景:
前端自动化测试经常需要附加图片或html,在适当的地方,适当的时机截图解决:
@allure.attach显示许多不同类型的提供的附件,可以补充测试,步骤或测试结果。
步骤:
在测试报告里附加网页:
allure.attach(body(内容), name, attachment_type, extension):
allure.attach('<head></head><body>首页</body>', '这是错误页的结果信息', allure.attachment_type.HTML)
在测试报告里附加图片:
allure.attach.file(source, name, attachment_type, extension):
allure.attach.file("./result/b.png",attachment_type=allure.attachment_type.PNG)
pytest测试实战关键点说明
pytestfixture
Fixture是在测试函数运行前后,由pytest执行的外壳函数,代码可以定制,满足多变的测试需求,功能包括:
Fixture是pytest用于将测试前后进行预备,清理工作的代码分离出核心测试逻辑的一种机制
pytestfixture用法
Fixture是为了测试用例的执行,初始化一些数据和方法
- -setup-show 回溯fixture的执行过程
自动化应用场景1:
场景:
步骤:
1.导入pytest
2.在登陆的函数上面加@pytest.fixture()
3.在要使用的测试方法中传入(登陆函数名称),就先登陆
4.不传入的就不登陆直接执行测试方法。
conftest.py
场景:
解决:
前提:
执行:
步骤:
conftest.py用法
conftest.py配置需要注意:
yaml的应用
test_demo.py:
#coding=utf-8
import pytest
import yaml
def get_datas():
with open("./data/data.yml") as f:
datas = yaml.safe_load(f)
return datas
class TestCal:
@pytest.mark.parametrize('a,b,expect', get_datas()['add_int']['datas'])
def test_add_int(self, calculate, a, b, expect):
assert expect == calculate.add(a, b)
@pytest.mark.parametrize('a,b,expect', get_datas()['add_float']['datas'], ids=get_datas()['add_float']['ids'])
def test_add_float(self, calculate, a, b, expect):
assert expect == round(calculate.add(a, b), 2)
def test_div(self, calculate):
with pytest.raises(ZeroDivisionError):
calculate.div(1, 0)
data.yml:
add_int:
datas:
- [10, 10, 20]
- [1, 1, 2]
add_float:
datas:
- [0.1, 0.1, 0.2]
-[0.1, 0.2, 0.3]
ids:
- 'float1'
- 'float2'
conftest.py:
#coding=utf-8
import pytest
from calculator import Calculator
@pytest.fixture()
def calculate():
print("开始计算")
cal = Calculator()
yield cal
print("结束计算")
calculator.py:
#coding:utf-8
class Calculator:
def add(self, a, b):
return a + b
def div(self, a, b):
return a / b