cls.driver.quit()
def test_01(self):
“”“定位失败截图案例”“”
self.driver.get(“https://www.baidu.com”)
self.driver.find_element_by_id(‘anjing’).send_keys(‘百度一下’)
self.driver.find_element_by_id(‘su’).click()
self.assertTrue(True)
def test_02(self):
“”“成功案例”“”
self.driver.get(“https://www.baidu.com”)
self.assertIn(“百度一下,你就知道”,self.driver.title)
if__name__==“__main__”:
testunit=unittest.TestSuite()
#加载用例
testunit.addTests(unittest.TestLoader().loadTestsFromTestCase(Test))
#执行用例
runner=HTMLTestRunner_cn.HTMLTestRunner(title=“自动化测试报告”,
description=“测试结果”,
stream=open(“result.html”,“wb”),
)
runner.run(testunit)
代码编写完成后,直接进行执行程序,执行完成后,就会发现我们测试报告已经生成了,直接打开就可以看到自动化测试结果,失败的存在截图信息。
Pytest
Pytest是一个使创建简单及可扩展性测试用例变得非常方便的框架,测试用例清晰、易读而无需大量的繁琐代码。
只要几分钟你就可以对你的应用程序或者库展开一个小型的单元测试或者复杂的功能测试。Pytest支持第三方插件,灵活性较高。
这里小编通过Pytest生成的Allure报告中添加测试报告内容。其中Allure属于一种开源的测试框架,可以基于多种语言进行使用,如Java的JUnit、Python的Pytest。
alluer生成的报告内容比较美观,目前测试中常用的测试报告。Allure的环境是基于Java环境的,前提条件需要将Java的环境安装完成(下载JDK,配置环境变量)。
下载地址:https://github.com/allure-framework/allure2(具体安装不在过多介绍)。
我们需要在conftest.py文件中去收集Pytest测试用例中失败的结果,小编这里选择用pytest_runtest_makereport函数来帮忙实现。
#conftest.py
from selenium import webdriver
import pytest
import time
import allure
te=time.strftime(“%Y-%m-%d%H:%M:%S”,time.localtime())
@pytest.hookimpl(tryfirst=True,hookwrapper=True)
def pytest_runtest_makereport(item,call):
#获取钩子方法的调用结果
outcome=yield
rep=outcome.get_result()
#仅仅获取用例call执行结果是失败的情况
if rep.when=="call"and rep.failed:
#添加allure报告截图
with allure.step(‘失败截图:’):
#将截图添加到报告中,图片名称已当前时间。
allure.attach(d.get_screenshot_as_png(),name=te,
attachment_type=allure.attachment_type.PNG)
#配置浏览器驱动,不显示浏览器执行过程
@pytest.fixture(scope=“session”)
def driver():
global d
options=webdriver.ChromeOptions()
options.add_argument(“–headless”)
d=webdriver.Chrome(options=options)
yield d
d.quit()
编写两个简单的测试用例,一个测试用例失败,一个测试用例成功。
#coding:utf-8
def test_01(driver):
#失败案例
driver.get(‘https://www.baidu.com/’)
driver.find_element_by_id(‘kw’).send_keys(‘51testing软件测试网’)
t=driver.title
assert t==‘51testing软件测试网’
def test_02(driver):
#成功案例
driver.get(‘https://www.baidu.com/’)
driver.find_element_by_id(‘kw’).send_keys(‘安静’)
t=driver.title
assert t==‘百度一下,你就知道’
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
6050802)]
[外链图片转存中…(img-QnaVeB9s-1719236050803)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!