目录
1.生成HTML测试报告
import unittest
from HTMLTestRunner import HTMLTestRunner
#定义测试用例的目录为当前目录中的test_case/目录
test_dir ='../20211220'
suits=unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
if __name__=="__main__":
#生成HTML格式的报告
fp = open('./test_report/result.html','wb')
runner = HTMLTestRunner(stream=fp,title="百度搜索测试报告",description="运行环境:windows10,Chrome浏览器")
runner.run(suits)
fp.close()
2.测试报告文件名
import unittest
import time
from HTMLTestRunner import HTMLTestRunner
#定义测试用例的目录为当前目录中的test_case/目录
test_dir ='../20211220'
suits=unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
if __name__=="__main__":
#生成HTML格式的报告
now_time = time.strftime("%Y-%m-%d %H_%M_%S")
fp = open('./test_report/'+now_time+'result.html','wb')
runner = HTMLTestRunner(stream=fp,title="百度搜索测试报告",description="运行环境:windows10,Chrome浏览器")
runner.run(suits)
fp.close()
3.数据驱动
import csv
import codecs
import unittest
from time import sleep
from itertools import islice
from selenium import webdriver
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "http://www.baidu.com"
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def baidu_search(self,search_key):
self.driver.get(self.base_url)
self.driver.find_element_by_id("kw").send_keys(search_key)
self.driver.find_element_by_id("su").click()
sleep(3)
def test_search(self):
with codecs.open('baidu_data.csv','r','utf_8_sig') as f:
data = csv.reader(f)
for line in islice(data,1,None):
search_key = line[1]
self.baidu_search(search_key)
if __name__ == '__main__':
unittest.main(verbosity=2)
4.避免单个失败
import csv
import codecs
import unittest
from time import sleep
from itertools import islice
from selenium import webdriver
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "http://www.baidu.com"
cls.test_data= []
with codecs.open('baidu_data.csv', 'r', 'utf_8_sig') as f:
data = csv.reader(f)
for line in islice(data, 1, None):
cls.test_data.append(line)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def baidu_search(self,search_key):
self.driver.get(self.base_url)
self.driver.find_element_by_id("kw").send_keys(search_key)
self.driver.find_element_by_id("su").click()
sleep(3)
def test_search_selenium(self):
self.baidu_search(self.test_data[0][1])
def test_search_unittest(self):
self.baidu_search(self.test_data[1][1])
def test_search_parameterized(self):
self.baidu_search(self.test_data[2][1])
if __name__ == '__main__':
unittest.main(verbosity=2)
5.Paramterized
import unittest
from time import sleep
from selenium import webdriver
from parameterized import parameterized
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "https://www.baidu.com"
def baidu_search(self,search_key):
self.driver.get(self.base_url)
self.driver.find_element_by_id("kw").send_keys(search_key)
self.driver.find_element_by_id("su").click()
sleep(2)
#通过Parameterized实现参数化
@parameterized.expand([
("case1","selenium"),
("case2","unittest"),
("case3","parameterized"),
])
def test_search(self,name,search_key):
self.baidu_search(search_key)
self.assertEqual(self.driver.title,search_key+"_百度搜索")
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)
6.DDT
import unittest
from time import sleep
from selenium import webdriver
from ddt import ddt,data,file_data,unpack
@ddt
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "https://www.baidu.com"
def baidu_search(self,search_key):
self.driver.get(self.base_url)
self.driver.find_element_by_id("kw").send_keys(search_key)
self.driver.find_element_by_id("su").click()
sleep(3)
#参数化使用方式一
@data(["case1","selenium"],["case2","ddt"],["case3","python"])
@unpack
def test_search1(self,case,search_key):
print("第一组测试用例: ",case)
self.baidu_search(search_key)
self.assertEqual(self.driver.title,search_key+"_百度搜索")
#参数化使用方式二
@data(("case1","selenium"),("case2","ddt"),("case3","python"))
@unpack
def test_search2(self, case, search_key):
print("第二组测试用例: ", case)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
#参数化使用方式三
@data({"search_key": "selenium"},{"search_key": "ddt"},{"search_key": "python"})
@unpack
def test_search3(self,search_key):
print("第三组测试用例: ",search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title,search_key + "_百度搜索")
#参数化读取JSON文件
@file_data('ddt_data_file.json')
def test_search4(self,search_key):
print("第四组测试用例: ",search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title,search_key + "_百度搜索")
# 参数化读取yaml文件
@file_data('ddt_data_file.yaml')
def test_search5(self,case):
search_key = case[0]["search_key"]
print("第五组测试用例: ", search_key)
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity=2)
7.Python自带的发送邮件功能
import smtplib
from email.mime.text import MIMEText
from email.header import Header
subject = 'Python email test'
#编写HTML类型的邮件正文
msg = MIMEText('你好', 'text','utf-8')
msg['Subject'] = Header(subject,'utf-8')
msg['From'] = Header("sender@sina.com")
msg['To'] = Header("receiver@qq.com")
#发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.sina.com")
smtp.login("sender@sina.com", "test")
smtp.sendmail("sender@sina.com", "receiver@qq.com", msg.as_string())
smtp.quit()
8.发送带附件的邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#邮件主题
subject='Python send email test'
#发送的附件
with open('log.txt','rb') as f:
send_att = f.read()
att = MIMEText(send_att, 'text','utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename="log.txt"'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = "sender@sina.com"
msg.attach(att)
#发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.sina.com")
smtp.login("sender@sina.com", "test")
smtp.sendmail("sender@sina.com", "receiver@qq.com", msg.as_string())
smtp.quit()
9.用yagmail发送文件
import yagmail
#连接邮箱服务器
yag = yagmail.SMTP(user="sender@sina.com",password="test",host = "smtp.sina.com")
#邮件正文
subject = "主题,自动化测试报告"
contents = ['Hello','World']
#发送邮件
yag.send('receiver.com',subject,contents)
10.整合自动发送邮件功能
import time
import unittest
import yagmail
from HTMLTestRunner import HTMLTestRunner
#把测试报告作为附件发送到指定邮箱
def send_mail(report):
yag = yagmail.SMTP(user="sender@sina.com",password="test",host='smtp.sina.com')
subject = "主题,自动化测试报告"
contents = "正文,请查看附件"
yag.send('receiver@qq.com',subject,contents,report)
print('email has send out !')
if __name__ == '__main__':
#定义测试用例的目录为当前目录
test_dir ='.'
suit = unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')
#获取当前日期和时间
now_time = time.strftime("%Y-%m-%d %H_%M_%S")
html_report='./test_report'+now_time+'result.html'
fp = open(html_report,'wb')
#调用HTMLTestRunner,运行测试用例
runner = HTMLTestRunner(stream=fp,title="百度搜索测试报告",description="运行环境:windows10,Chrome浏览器")
runner.run(suit)
fp.close()
send_mail(html_report)