目录
BeautifulReport库
任务要求
测试报告截图
完整代码
BeautifulReport库
BeautifulReport 是一个基于 Python 编写的轻量级报告框架,它可以通过在测试用例中添加模板配置文件,生成美观且易读的 HTML 测试报告。以下是使用 BeautifulReport 的步骤:
安装 BeautifulReport:
pip install beautifulreport
引入 BeautifulReport:
from BeautifulReport import BeautifulReport
在测试用例中添加模板配置文件,并实例化 BeautifulReport:
br = BeautifulReport(driver, '测试报告名称', '用例执行环境')
执行测试用例并生成报告
br.report(description='测试描述', filename='测试报告文件名.html')
完整示例代码如下:
import unittest
from BeautifulReport import BeautifulReport
class Test(unittest.TestCase):
def test_case(self):
# 这里写你的测试用例
pass
if __name__ == '__main__':
suite = unittest.defaultTestLoader.discover('.', pattern='test*.py')
br = BeautifulReport(suite)
br.report(description='测试描述', filename='测试报告.html')
注意:在生成报告时,需要将 filename 参数设置为 .html 文件类型。
另外,如果需要给测试报告增加 LOGO 图片,可以将图片文件放置于当前工程目录下或其他指定目录,在实例化 BeautifulReport 对象时通过 logo_path 参数指定路径即可。
任务要求
使用测试驱动方式设计和编写对应的测试用例和编写脚本,脚本具体补助:
(1)进入企业网站管理内容系统后台登录页面。
(2)在登录页面输入账号和验证码,但是不输入密码。
(3)获取页面上的错误提示信息
(4)使用断言判断获取到的提示信息是否与预期的提示信息一致
(5)如果不一致,则自动对界面进行截图并保存
测试用例至少需要覆盖下面情况:
1、正确输入可以登录 2、用户名没有输入 3、密码没有输入 4、验证码没有输入 5、用户名密码不匹配
测试报告截图
![](https://img-blog.csdnimg.cn/a5e1c68c73b4455082f31b12734c1b52.png)
完整代码
import datetime
from unittest import TestCase
from selenium import webdriver
import unittest
import time
import os
from BeautifulReport import BeautifulReport
from selenium.webdriver.common.by import By
class TestWork(TestCase):
def setUp(self) -> None:
print('开始测试')
self.driver = webdriver.Chrome(executable_path='../chromedriver.exe')
# self.driver.maximize_window()
self.driver.get('http://8 /ms')
def tearDown(self) -> None:
print('测试结束')
self.driver.quit()
# @BeautifulReport.add_test_img('test1') # 装饰器表示可以将该测试函数中抛出异常时的页面截图加入测试报告中。
def test1(self):
"""正确输入可以登录"""
try:
print('test_one: 当前的地址是:', self.driver.current_url, ' 标题是:', self.driver.title)
self.driver.find_element(By.XPATH, '//*[@id="input-name"]').send_keys('administrator')
self.driver.find_element(By.XPATH, '//*[@id="input-password"]').send_keys('administrator123')
self.driver.find_element(By.XPATH, '//*[@id="input-rand-code"]').send_keys('abcd')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/button').click()
time.sleep(2)
self.assertEqual(self.driver.current_url, 'http://8. 4.79/ms/index.do', '成功登录')
except:
self.driver.get_screenshot_as_file('./img/test_work_test1.png')
print('case1:测试执行失败!')
raise
# @BeautifulReport.add_test_img('test2')
def test2(self):
"""用户名没有输入"""
try:
print('test_two: 当前的地址是:', self.driver.current_url, ' 标题是:', self.driver.title)
# self.driver.find_element(By.XPATH, '//*[@id="input-name"]').send_keys('a*****3')
self.driver.find_element(By.XPATH, '//*[@id="input-password"]').send_keys('a*****3')
self.driver.find_element(By.XPATH, '//*[@id="input-rand-code"]').send_keys('abcd')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/button').click()
username = self.driver.find_element(By.XPATH,
'//*[@id="app"]/div/div/div[2]/div/form/div[1]/div/div/div').text
self.assertEqual(username, '请输入正确的用户名')
time.sleep(2)
except:
self.driver.get_screenshot_as_file('./img/test_work_test2.png')
print('case2:测试执行失败!')
raise
# @BeautifulReport.add_test_img('test3')
def test3(self):
"""密码没有输入"""
try:
print('test_three: 当前的地址是:', self.driver.current_url, ' 标题是:', self.driver.title)
self.driver.find_element(By.XPATH, '//*[@id="input-name"]').send_keys(tor')
# self.driver.find_element(By.XPATH, '//*[@id="input-password"]').send_keys('123')
self.driver.find_element(By.XPATH, '//*[@id="input-rand-code"]').send_keys('abcd')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/button').click()
password = self.driver.find_element(By.XPATH,
'//*[@id="app"]/div/div/div[2]/div/form/div[2]/div/div/div').text
self.assertEqual(password, '请输入密码')
time.sleep(2)
except:
self.driver.get_screenshot_as_file('./img/test_work_test3.png')
print('case3:测试执行失败!')
raise
# @BeautifulReport.add_test_img('test4')
def test4(self):
"""验证码没有输入"""
try:
print('test_four: 当前的地址是:', self.driver.current_url, ' 标题是:', self.driver.title)
self.driver.find_element(By.XPATH, '//*[@id="input-name"]').send_keys('a*******')
self.driver.find_element(By.XPATH, '//*[@id="input-password"]').send_keys('a*****3')
# self.driver.find_element(By.XPATH, '//*[@id="input-rand-code"]').send_keys('abcd')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/button').click()
captcha = self.driver.find_element(By.XPATH,
'//*[@id="app"]/div/div/div[2]/div/form/div[3]/div[1]/div/div/div').text
self.assertEqual(captcha, "请输入验证码")
time.sleep(2)
except:
self.driver.get_screenshot_as_file('./img/test_work_test4.png')
print('case4:测试执行失败!')
raise
# @BeautifulReport.add_test_img('test5')
def test5(self):
"""用户名密码不匹配"""
try:
print('test_five: 当前的地址是:', self.driver.current_url, ' 标题是:', self.driver.title)
self.driver.find_element(By.XPATH, '//*[@id="input-name"]').send_keys('a*****3')
self.driver.find_element(By.XPATH, '//*[@id="input-password"]').send_keys('a*****3')
self.driver.find_element(By.XPATH, '//*[@id="input-rand-code"]').send_keys('abcd')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div[2]/div/button').click()
time.sleep(2)
error = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/p').text
self.assertEqual(error, "请输入正确的用户名和密码")
time.sleep(2)
except:
self.driver.get_screenshot_as_file('./img/test_work_test5.png')
print('case5:测试执行失败!')
raise
if __name__ == '__main__':
# # 指定测试套件(可选)
suite = unittest.TestLoader().loadTestsFromTestCase(TestWork)
# 执行测试
run_result = BeautifulReport(suite)
run_result.add_test_img('./img/test_work_test1.png')
run_result.add_test_img('./img/test_work_test2.png')
run_result.add_test_img('./img/test_work_test3.png')
run_result.add_test_img('./img/test_work_test4.png')
run_result.add_test_img('./img/test_work_test5.png')
run_result.report(filename='report.html', description='测试报告', log_path='./Test_cases',
report_dir='./Test_reports')
|