一.
下载修改过得HTMLTestRunner python脚本
密码: 8v7y
如果使用 virtualenv 来创建虚拟环境可将HTMLTestRunner两个文件放到 path: ~/.virtualenvs/virtualenv(创建环境名称)/lib/python3.5 中
下载Selenium 谷歌浏览器/火狐浏览器webdriver插件
Chrome:http://chromedriver.storage.googleapis.com/index.html
Firefox:https://github.com/mozilla/geckodriver/releases
二.
结构目录
start_end.py 建立测试用例起始源
from selenium import webdriver
import unittest
from time import sleep
# 第一种unittest起始源
class Test_star(unittest.TestCase):
def setUp(self):
print('test start')
def tearDown(self):
print('test end')
from selenium.webdriver import ActionChains
# 第一种unittest+selenium起始源
class Test_selenium(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(
executable_path='./chromedriver',
)
self.driver.implicitly_wait(10)
self.driver.get('https://www.baidu.com')
def tearDown(self):
self.driver.quit()
# 测试selenium跳转小案例
"""
from selenium import webdriver
import unittest
from time import sleep
from selenium.webdriver import ActionChains
class Test_star(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(
executable_path='./chromedriver',
)
self.driver.implicitly_wait(10)
self.driver.get('https://www.baidu.com')
def test_baidu(self):
driver = self.driver
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("Selenium 我要自学网")
driver.find_element_by_id("su").click()
sleep(3)
title = driver.title
self.assertEqual(title,"Selenium 我要自学网_百度搜索")
driver.find_element_by_partial_link_text("自动化测试教程-").click()
sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
"""
calculatory.py 创建第一种实例方法为(开发代码),方便后期(测试)传参数调用.
# 做一个开发通用小案例 方便测试后期传参调用
class Count:
def __init__(self,a,b):
self.a = int(a)
self.b = int(b)
def add(self):
return self.a + self.b
def sub(self):
return self.a - self.b
test_add.py , test_sub.py 创建两个测试脚本 调用 start_end.py , calculatory.py方法:
test_add.py :
from start_end import * # start_end.py
from calculatory import * # calculatory.py
# @unittest.skip("skip ")
# 继承Test_star方法
class Test_add(Test_star):
def test_add(self):
# 调用calculatory.py的Count()开发实例
# 并传递参数交给Test_star执行
j = Count(5, 5)
# 用unittest下的assertEqual进行两值判断(10)可以自行输入
self.assertEqual(j.add(), 10)
print('@@@@@@@@@@@@@@@@@@@@ok1')
test_sub.py :
from start_end import *
from calculatory import *
# 跳过实例
# @unittest.skip("skip ")
# 条件为真跳过测试
# @unittest.skipIf("skip ")
# @unittest.skipUnless 条件为假
# @unittest.expectedFailure 预期设置失败
class Test_sub(Test_star):
def test_sub(self):
j = Count(5, 5)
self.assertEqual(j.sub(), 0)
print('ok2********')
test_selenium.py 创建第二种实例方法为Selenium点击实例 调用start_end.py 的第二种方法
from start_end import * # start_end.py
# 调用 start_end.py下的Test_selenium测试起源方法
class test_test(Test_selenium):
def test_baidu(self):
driver = self.driver
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("Selenium 我要自学网")
driver.find_element_by_id("su").click()
sleep(3)
title = driver.title
self.assertEqual(title, "Selenium 我要自学网_百度搜索")
driver.find_element_by_partial_link_text("自动化测试教程-").click()
sleep(5)
print('**************成功*****************')
通过send_request.py 来实现test_add.py , test_sub.py , test_selenium.py 三个测试文件的脚本运行!并生成HTMLTestRunner测试报告!!!
import unittest
# 导入下载好的两个文件 两个文件路径在: ~/.virtualenvs/virtualenv(创建环境名称)/lib/python3.5
from BSTestRunner import BSTestRunner
import time
# 指定运行文件路径 指运行test以下的所有.py文件
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*py')
if __name__ == '__main__':
# runner = unittest.TextTestRunner()
# runner.run(discover)
# 指定生成文件名字
report_dir = './'
now = time.strftime("%Y-%m-%d %H_%M_%S")
report_name = report_dir + '/' + now +'result.html'
# 保存本地并生成报告 关闭
with open(report_name,'wb') as f:
runner = BSTestRunner(stream=f,title="test Report",description="Test case result")
runner.run(discover)
f.close()
生成的测试报告
运行结果:
实现将测试报告最新日期发送到指定邮箱
import unittest
from BSTestRunner import BSTestRunner
import time
import os
import smtplib # 发送邮件模块
from email.mime.text import MIMEText # 定义邮件内容
from email.header import Header # 定义邮件标题
from email.mime.multipart import MIMEMultipart
# 找出最新报告
def latest_report(report_dir):
lists = os.listdir(report_dir)
# 按时间顺序对该目录文件夹下面的文件进行排序
lists.sort(key=lambda fn: os.path.getatime(report_dir + '/' + fn))
file = os.path.join(report_dir, lists[-1])
# print(file[12:])
return file
# 将测试报告发送邮箱
def send_mail(latest_report):
# 读取测试报告内容
f = open(latest_report,'rb')
mail_content = f.read()
# print(latest_report.file)
# print(mail_content)
f.close()
# 发送邮件
# 发送邮箱服务器
smtpserver = "smtp.qq.com"
# 发送邮箱用户名密码
user = "lbc.star@foxmail.com"
# smp qq邮箱指定授权码
password = "*********"
# 发送和接收邮箱
sender = "lbc.star@foxmail.com"
receive = "kaifachengxu@126.com"
# 多用户发送
# receive = ["kaifachengxu@126.com","1310276776@qq.com"]
# 发送邮件主题
subject = "自动化测试报告"
# 构造附件内容
send_file = open(latest_report, 'rb').read()
att = MIMEText(send_file, 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 取文件名作为 发送附件的名字
att["Content-Disposition"] = 'attachment;filename="{}"'.format(latest_report[12:])
msgRoot = MIMEMultipart()
msgRoot.attach(MIMEText(mail_content, 'html', 'utf-8'))
msgRoot['Subject'] = Header(subject, 'utf-8')
msgRoot['From'] = "lbc.star@foxmail.com"
msgRoot['To'] = receive
msgRoot.attach(att)
# SSL协议端口号使用456
smtp = smtplib.SMTP_SSL(smtpserver, 465)
# 向服务器标识用户身份
smtp.helo(smtpserver)
# 服务器返回结果确认
smtp.ehlo(smtpserver)
# 登录邮箱账户密码
smtp.login(user, password)
print('开始发送邮件')
smtp.sendmail(sender, receive, msgRoot.as_string())
smtp.quit()
print('邮件发送完成')
if __name__ == '__main__':
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*py')
# runner = unittest.TextTestRunner()
# runner.run(discover)
# 生成测试报告的路径
report_dir = './test_file'
now = time.strftime("%Y-%m-%d %H_%M_%S")
report_name = report_dir + '/' + now +'result.html'
with open(report_name,'wb') as f:
runner = BSTestRunner(stream=f,title="test Report",description="Test case result")
runner.run(discover)
f.close()
# 执行找出最新报告
latest_report = latest_report(report_dir)
# 发送邮件
send_mail(latest_report)
运行结果: