自动化测试中的失败截图和存log_allure失败时截图,2024年最新分享两道阿里P7究极难度算法题

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
img

正文

然后执行when=‘call’ 返回call 的执行结果

最后执行when='teardown’返回teardown 的执行结果

例如:

conftest.py

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print(‘------------------------------------’)

获取钩子方法的调用结果

out = yield
print(‘用例执行结果’, out)

3. 从钩子方法的调用结果中获取测试报告

report = out.get_result()

print(‘测试报告:%s’ % report)
print(‘步骤:%s’ % report.when)
print(‘nodeid:%s’ % report.nodeid)
print(‘description:%s’ % str(item.function.doc))
print((‘运行结果: %s’ % report.outcome))

运行用例的过程会经历三个阶段:setup-call-teardown,每个阶段都会返回的 Result 对象和 TestReport 对象,以及对象属性。

如果setup执行失败了,setup的执行结果的failed,后面的call用例和teardown都不会执行了。

如果setup正常执行,但是测试用例call失败了。那么此时运行的结果就是failed。

如果setup正常执行,测试用例call正常执行,teardown失败了,这种情况,最终统计的结果:1 passed, 1 error in 0.16 seconds

只获取call的时候,我们在写用例的时候,如果保证setup和teardown不报错情况,只关注测试用例本身的运行结果,前面的 pytest_runtest_makereport 钩子方法执行了三次。

可以加个判断:if report.when == “call”

import pytest
from _pytest import runner
‘’’

对应源码

def pytest_runtest_makereport(item, call):
“”" return a :py:class:_pytest.runner.TestReport object
for the given :py:class:pytest.Item and
:py:class:_pytest.runner.CallInfo.
“”"
‘’’

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print(‘------------------------------------’)

获取钩子方法的调用结果

out = yield

print(‘用例执行结果’, out)

3. 从钩子方法的调用结果中获取测试报告

report = out.get_result()
if report.when == “call”:
print(‘测试报告:%s’ % report)
print(‘步骤:%s’ % report.when)
print(‘nodeid:%s’ % report.nodeid)
print(‘description:%s’ % str(item.function.doc))
print((‘运行结果: %s’ % report.outcome))

@pytest.fixture(scope=“session”, autouse=True)
def fix_a():
print(“setup 前置操作”)
yield
print(“teardown 后置操作”)

allure报告集成错误截图 需要使用conftest.py文件,conftest.py需要存在在测试目录中,文件名不能变更,可以根据模块创建层级嵌套。

具体参照pytest的官方文档

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
‘’’
hook pytest失败
:param item:
:param call:
:return:
‘’’

execute all other hooks to obtain the report object

outcome = yield
rep = outcome.get_result()

we only look at actual failing test calls, not setup/teardown

if rep.when == “call” and rep.failed:
mode = “a” if os.path.exists(“failures”) else “w”
with open(“failures”, mode) as f:

let’s also access a fixture for the fun of it

if “tmpdir” in item.fixturenames:
extra = " (%s)" % item.funcargs[“tmpdir”]
else:
extra = “”
f.write(rep.nodeid + extra + “\n”)

pic_info = adb_screen_shot()

with allure.step(‘添加失败截图…’):
allure.attach(driver.get_screenshot_as_png(), “失败截图”, allure.attachment_type.PNG)

好了,我们可以用在我们自己的项目里面来了。 我们可以在conftest.py里面定义:

import pytest
from selenium import webdriver
import os
import allure

_driver = None

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
‘’’
获取每个用例状态的钩子函数
:param item:
:param call:
:return:
‘’’

获取钩子方法的调用结果

outcome = yield
rep = outcome.get_result()

仅仅获取用例call 执行结果是失败的情况, 不包含 setup/teardown

if rep.when == “call” and rep.failed:
mode = “a” if os.path.exists(“failures”) else “w”
with open(“failures”, mode) as f:

let’s also access a fixture for the fun of it

if “tmpdir” in item.fixturenames:
extra = " (%s)" % item.funcargs[“tmpdir”]
else:
extra = “”
f.write(rep.nodeid + extra + “\n”)

添加allure报告截图

if hasattr(_driver, “get_screenshot_as_png”):
with allure.step(‘添加失败截图…’):
allure.attach(_driver.get_screenshot_as_png(), “失败截图”, allure.attachment_type.PNG)

@pytest.fixture(scope=‘session’)
def browser():
global _driver
if _driver is None:
_driver =webdriver.Chrome()
yield _driver
print(“1111111111”)
_driver.quit()

然后写一个测试用例,如在某度上搜一个关键词。

@allure.feature(‘self study’)
class TestLesson():
@allure.story(‘user course page’)
@allure.description(‘be course’)
def test_be_ge_course(self,browser):
url = ‘http://www.baidu.com’
browser.get(url)
time.sleep(2)
browser.find_element_by_id(‘kw’).send_keys(“python”)

with allure.step(‘查找元素…’):
browser.find_element_by_id(‘su’).click()
time.sleep(2)

assert browser.title == ‘python’

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-k1k2OXpQ-1713597098833)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 30
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值