一、测试用例
1、以表格的形式展示
2、以思维导图的形式展示
二、自动化测试
1、登录测试
1.测试数据
2.测试代码
import csv
import sys
from ddt import ddt, data, unpack ,file_data
from selenium import webdriver
import time
import unittest
# 对测试文件进行读取
def loginText(file_name):
rows = []
path = sys.path[0]
# Test_login.txt 所在的路径:脚本路径中的data文件夹里
with open(path+'/data/'+file_name,'r',encoding='UTF-8') as f:
readers = csv.reader(f,delimiter=',',quotechar='|')
next(readers,None)
for row in readers:
temprows=[]
for i in row:
temprows.append(i)
rows.append(temprows)
return rows
# 数据驱动(ddt)
@ddt
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.url = "http://127.0.0.1:8080/login.html"
self.driver.maximize_window()
time.sleep(2)
@data(*loginText('Test_login.txt'))
@unpack
def test_login(self,username,password):
driver = self.driver
url = self.url
driver.get(url)
# 清除账号与密码的输入框
driver.find_element_by_id("username").clear()
driver.find_element_by_id("password").clear()
driver.find_element_by_id("username").send_keys(username)
driver.find_element_by_id("password").send_keys(password)
time.sleep(2)
driver.find_element_by_id("submit").click()
time.sleep(1)
try:
# 定位弹框(获取弹框的操作句柄)
alert = driver.switch_to.alert
# 点击弹框中的确认按钮
alert.accept()
# 登录成功的标题为:在线音乐
self.assertEqual("在线音乐", driver.title, msg="登录失败")
except:
# 登录成功的标题为:在线音乐
self.assertEqual("在线音乐", driver.title, msg="登录失败")
time.sleep(2)
def tearDown(self):
self.driver.quit()
time.sleep(2)
if __name__ == '__main__':
unittest.main()
3.测试结果
2、注册测试
1.测试数据
2.测试代码
import csv
import sys
from ddt import ddt, data, unpack ,file_data
from selenium import webdriver
import time
import unittest
# 对测试文件进行读取
def resulterText(file_name):
rows = []
path = sys.path[0]
# Test_login.txt 所在的路径:脚本路径中的data文件夹里
with open(path+'/data/'+file_name,'r',encoding='UTF-8') as f:
readers = csv.reader(f,delimiter=',',quotechar='|')
next(readers,None)
for row in readers:
temprows=[]
for i in row:
temprows.append(i)
rows.append(temprows)
return rows
@ddt
class register(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.url = "http://127.0.0.1:8080/login.html"
url = self.url
driver = self.driver
driver.get(url)
driver.find_element_by_id("register").click()
time.sleep(2)
self.driver.maximize_window()
time.sleep(2)
@data(*resulterText('Test_result.txt'))
@unpack
def test_result(self,username,password,repassword):
driver = self.driver
driver.get("http://127.0.0.1:8080/register.html")
driver.find_element_by_id("username").clear()
driver.find_element_by_id("password").clear()
driver.find_element_by_id("repassword").clear()
driver.find_element_by_id("username").send_keys(username)
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_id("repassword").send_keys(repassword)
time.sleep(2)
driver.find_element_by_id("register").click()
time.sleep(2)
try:
# 定位弹框(获取弹框的操作句柄)
alert = driver.switch_to.alert
driver.implicitly_wait(10)
# 操作获取的句柄关闭 alter
alert.accept()
self.assertEqual("音乐播放器登录页面", driver.title, msg="注册失败")
except:
self.assertEqual("音乐播放器登录页面",driver.title,msg="注册失败")
def tearDown(self):
self.driver.quit()
time.sleep(2)
if __name__ == '__main__':
unittest.main()
3.测试结果
3、上传文件测试
1.测试数据
2.测试代码
import csv
import sys
from ddt import ddt, data, unpack, file_data
from selenium import webdriver
import os
import time
import unittest
# 对测试文件进行读取
def uploadText(file_name):
rows = []
path = sys.path[0]
with open(path + '/data/' + file_name, 'r', encoding='UTF-8') as f:
readers = csv.reader(f, delimiter=',', quotechar='|')
next(readers, None)
for row in readers:
temprows = []
for i in row:
temprows.append(i)
rows.append(temprows)
return rows
@ddt
class Upload(unittest.TestCase):
# 一个测试用例执行完以后,需要重新登录才可以上传文件
def setUp(self):
self.driver = webdriver.Firefox()
self.url = "http://127.0.0.1:8080/login.html"
self.driver.maximize_window()
url = self.url
driver = self.driver
driver.get(url)
driver.find_element_by_id("username").clear()
driver.find_element_by_id("password").clear()
driver.find_element_by_id("username").send_keys("admin")
driver.find_element_by_id("password").send_keys("123456")
time.sleep(2)
driver.find_element_by_id("submit").click()
time.sleep(2)
@data(*uploadText('Test_upload.txt'))
@unpack
def test_upload(self, path, singer):
driver = self.driver
time.sleep(2)
driver.get("http://127.0.0.1:8080/list.html")
# 点击添加歌曲按钮
# time.sleep(10)
driver.find_element_by_xpath("/html/body/div/div[2]/a[2]").click()
driver.find_element_by_id("file").send_keys(os.path.abspath(path))
driver.find_element_by_id("singer").send_keys(singer)
time.sleep(2)
driver.find_element_by_xpath("//*[@id='submit']").click()
time.sleep(2)
self.assertEqual("在线音乐", driver.title, msg="上传文件失败")
def tearDown(self):
self.driver.quit()
time.sleep(2)
if __name__ == '__main__':
unittest.main()
3.测试结果
文章来源:https://www.toymoban.com/news/detail-509594.html
4、生成测试报告
1.代码
import HTMLTestRunner
import os
import sys
import unittest
import time
def createsuite():
# 测试以Test_开头,.py结尾的文件
disvovers = unittest.defaultTestLoader.discover("../Test",pattern="Test_*.py",top_level_dir=None)
print(disvovers)
return disvovers
if __name__ == '__main__':
# 创建HTML报告生成文件夹,获取当前脚本存放的路径
curpath = sys.path[0]
# 创建文件夹
if not os.path.exists(curpath+'/resultreport'):
os.makedirs(curpath+'/resultreport')
# 文件夹命名(以生成文件的时间进行命名)
now = time.strftime("%Y-%m-%d-%H %M %S",time.localtime(time.time()))
filename = curpath+'/resultreport/'+now+'resultreport.html'
with open(filename,'wb') as fp:
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"测试报告",description=u"用例执行情况",verbosity=2)
suite = createsuite() # 调用测试套件
runner.run(suite)
2.结果
文章来源地址https://www.toymoban.com/news/detail-509594.html
到了这里,关于在线音乐播放系统测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!